我正在尝试在 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();