您好,登錄后才能下訂單哦!
這篇文章主要介紹了PHP如何實現工廠模式,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
工廠模式
就是解決這樣的一些情況的設計方法。
工廠模式是一種類,建立了一個工廠來根據所需來創建對象,這種方式在多態性編程中是很重要的,允許動態替換類,修改配置等。
/*基本工廠模式代碼*/
<?php /** * 基本工廠模式 * */ class User { private $username; public function __construct($username) { $this->username = $username; } public function getUser() { return $this->username; } } class userFactory { static public function createUser() { return new User('Jack'); } } $user = userFactory::createUser();echo $user->getUser(); ?>
工廠模式分為:簡單工廠模式、工廠方法模式、抽象工廠模式。
簡單工廠模式,通過靜態方法創建對象。可以理解成,只負責生產同一等級結構中的任何一個產品,但是不能新增產品。
<?php /** *簡單工廠模式 * */ interface userProperties { function getUsername(); function getGender(); function getJob(); } class User implements userProperties{ private $username; private $gender; private $job; public function __construct($username, $gender, $job) { $this->username = $username; $this->gender = $gender; $this->job = $job; } public function getUsername() { return $this->username; } public function getGender() { return $this->gender; } public function getJob() { return $this->job; } } class userFactory { static public function createUser($properties = []) { return new User($properties['username'], $properties['gender'], $properties['job']); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = userFactory::createUser($employers[0]); echo $user->getUsername(); ?>
工廠方法模式,去掉了簡單工廠模式中方法的靜態屬性,使其可以被子類集成,定義一個創建對象的接口,讓子類去決定實例化哪個類。可以理解成,用來生產同一等級結構中的固定產品,但是支持增加產品。
<?php /** * 工廠方法模式 **/ interface userProperties { function getUsername(); function getGender(); function getJob(); } interface createUser { function create($properties); } class User implements userProperties{ private $username; private $gender; private $job; public function __construct($username, $gender, $job) { $this->username = $username; $this->gender = $gender; $this->job = $job; } public function getUsername() { return $this->username; } public function getGender() { return $this->gender; } public function getJob() { return $this->job; } } class userFactory { private $user; public function __construct($properties = []) { $this->user = new User($properties['username'], $properties['gender'], $properties['job']); } public function getUser() { return $this->user; } } class FactoryMan implements createUser { function create($properties) { return new userFactory($properties); } } class FactoryWoman implements createUser { function create($properties) { return new userFactory($properties); } } class clientUser { static public function getClient($properties) { $fac = new FactoryMan; $man = $fac->create($properties); echo $man->getUser()->getUsername(); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = clientUser::getClient($employers[0]); ?>
抽象工廠模式,提供一個創建一系列相關或者相互依賴的對象的接口。可以理解成,用來生產不用類型的全部產品,但是不能增加新品,支持增加新的類型。
<?php /** * 抽象工廠模式 * */ interface userProperties { function getUsername(); function getGender(); function getJob(); } interface createUser { //將對象的創建抽象成一個接口 function createOpen($properties);//內向創建 function createIntro($properties);//外向創建 } class User implements userProperties{ private $username; private $gender; private $job; public function __construct($username, $gender, $job) { $this->username = $username; $this->gender = $gender; $this->job = $job; } public function getUsername() { return $this->username; } public function getGender() { return $this->gender; } public function getJob() { return $this->job; } } class userFactory { private $user; public function __construct($properties = []) { $this->user = new User($properties['username'], $properties['gender'], $properties['job']); } public function getUser() { return $this->user; } } class FactoryMan implements createUser { function createOpen($properties) { return new userFactory($properties); } function createIntro($properties) { return new userFactory($properties); } } class FactoryWoman implements createUser { function createOpen($properties) { return new userFactory($properties); } function createIntro($properties) { return new userFactory($properties); } } class clientUser { static public function getClient($properties) { $fac = new FactoryMan; $man = $fac->createOpen($properties); echo $man->getUser()->getUsername(); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = clientUser::getClient($employers[0]); ?>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“PHP如何實現工廠模式”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。