2

我正在尝试在 PHP 中创建一个简单的工厂模式演示。我不确定我的代码是否是最佳实践。似乎我有一些重复的代码,但我不知道如何改进它。基本上,我想创建 3 种类型的帐户(基本、高级和 vip)。请指教。非常感谢。

抽象类

abstract class User {

    function __construct() {
        $this->db= new Database('mysql','localhost','mvcdb','root','');
    }

    abstract function checkUser();

    function showAccountCredit(){
        return $this->credits;
    }
    function getUserName(){
        return $this->username;
    }


}

我有 3 种不同的用户帐户类型:

基本账户

class BasicUser extends User {

    function __construct($username) {
        parent::__construct();
        $this->username=$username;
        $this->credit='10';
        $this->accountType='Basic Account';

        $data=$this->checkUser();

        if(!empty($data)){
            echo 'The username: '.$this->username.' already exists<br>';
            return false;
        }

        $array=array('username'=>$this->username, 'password'=>'password','credit'=> $this->credit,'accountType'=>$this->accountType);
        $this->db->insert('user',$array);

    }


     function checkUser(){

        $array=array(':username'=>$this->username);
        $results=$this->db->select('SELECT * FROM USER WHERE username=:username',$array);
        if(!empty($results)){
        $this->credit=$results[0]['credit'];
        $this->accountType=$results[0]['accountType'];
        }

        return $results;

    }

    function showAccountCredit() {
        echo 'Username: '.$this->username.'<br>';
        echo 'Account Credit: '.$this->credit.'<br>';
        echo 'Account Type: '.$this->accountType;
    }

}

至尊帐号

class PremiumUser extends User {

    function __construct($username) {
        parent::__construct();
        $this->username=$username;
        $this->credit='100';
        $this->accountType='Premium Account';

        $data=$this->checkUser();

        if(!empty($data)){
            echo 'The username: '.$this->username.' already exists<br>';
            return false;
        }

        $array=array('username'=>$this->username, 'password'=>'password','credit'=> $this-                   >credit,'accountType'=>$this->accountType);
        $this->db->insert('user',$array);

    }


     function checkUser(){

        $array=array(':username'=>$this->username);
        $results=$this->db->select('SELECT * FROM USER WHERE username=:username',$array);
        if(!empty($results)){
        $this->credit=$results[0]['credit'];
        $this->accountType=$results[0]['accountType'];
        }

        return $results;

    }

    function showAccountCredit() {
        echo 'Username: '.$this->username.'<br>';
        echo 'Account Credit: '.$this->credit.'<br>';
        echo 'Account Type: '.$this->accountType.'<br>';
    }

}

贵宾账户:

class VipUser extends User {

    function __construct($username) {
        parent::__construct();
        $this->username=$username;
        $this->credit='1000';
        $this->accountType='VIP Account';

        $data=$this->checkUser();

        if(!empty($data)){
            echo 'The username: '.$this->username.' already exists<br>';
            return false;
        }

        $array=array('username'=>$this->username, 'password'=>'password','credit'=> $this->credit,'accountType'=>$this->accountType);
        $this->db->insert('user',$array);

    }


     function checkUser(){

        $array=array(':username'=>$this->username);
        $results=$this->db->select('SELECT * FROM USER WHERE username=:username',$array);
        if(!empty($results)){
        $this->credit=$results[0]['credit'];
        $this->accountType=$results[0]['accountType'];
        }

        return $results;

    }

    function showAccountCredit() {
        echo 'Username: '.$this->username.'<br>';
        echo 'Account Credit: '.$this->credit.'<br>';
        echo 'Account Type: '.$this->accountType;
    }

}

用户工厂类

class UserFactory {

    static function create($username,$accountType){

        $accountType = strtolower($accountType);

        switch($accountType){
        case 'basic': return new BasicUser($username);
        case 'premium':return new PremiumUser($username);
        case 'vip': return new VipUser($username);
        default :return new BasicUser($username);
        }
    }

索引.php

$user1= UserFactory::create('Jerry', 'Vip');
$user1->showAccountCredit();


$user2= UserFactory::create('Bob', 'Basic');
$user2->showAccountCredit();


$user3= UserFactory::create('Betty', 'premium');
$user3->showAccountCredit();
4

1 回答 1

2

与其拥有 3 种不同的用户实现,不如考虑 3 种不同的“UserType”实现。分离职责并将对象组合在一起。这是另一种行动模式,称为strategy

function showAccountCredit(){
  return $this->type->showAccountCredit();
}

新类,UserType 代表特殊行为,然后 User 将包含其余的通用内容,减少您描述的重复(或至少大部分)

另一方面,工厂可用于从数据库加载对象:

class Loader{
  private $userTypeFactory;

  public function loadUser($id){
    $userRow = $db->loadRow();
    ...

    $userType = $userTypeFactory->$userRow["type"]();
    return new User($userType);
  }
}

class UserTypeFactory{
  public function vip(){
    return new VipUserType();
  }

  public function premium(){
    return new PremiumUser();
  }
}

你可以更进一步,引入 IoC 框架。看看symfony2。哦,请不要使用开关,而是像我一样使用多态性,动态调用。


编辑:

策略 == 包装行为。例如,如果您正在检查用户权限以查看优质内容,通常情况如下:

class PagesController{
  ...

  public function showPage($id){
    ...
    if ($user->type == "premium" || $user->type == "vip"){
      ...
    }
    else if ($user->type == "credit"){
      $user->credits--;
      updateUser();
    }
    else{
      die("You do not have permissions to see this content...");
    }

    ... //render page
  }
}

以下是使用“策略”的样子:

class PagesController{
  ...
  public function showPage($id){
    $page = ...;

    $user->pageRequested($page);

    //render page
  }
}

class User{
  private $userType;

  public function pageRequested($page){
    $this->userType->userRequestedPage($user, $page);
  }
}

class VipUserType{
  public function userRequestedPage($user, $page){
    //do nothing
  }
}

class PremiumUserType{
  public function userRequestedPage($user, $page){
    //do nothing
  }
}

class BasicUserType{
  public function userRequestedPage($user, $page){
    throw new Exception("You cant access this page");
  }
}

class CreditUserType{
  public function userRequestedPage($user, $page){
    $user->credit--;
    $user->save();
  }
}

这样,与 userType 相关的所有内容都是分离的,添加新类型很容易,而不会破坏现有类型。

顺便说一句:这只不过是普通的旧多态性。策略只是 OOP 基本要素的另一个闪亮词,就像大多数模式一样。不要浪费时间去思考模式,它们对灵感很有好处,但是你会从真正的 OOP 中获得更多的经验,看看 smalltalk - http://www.pharo-project.org/home

于 2012-05-16T21:54:25.573 回答