您好,登錄后才能下訂單哦!
這篇文章主要介紹了PHP如何實(shí)現(xiàn)工廠模式,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
工廠模式
就是解決這樣的一些情況的設(shè)計(jì)方法。
工廠模式是一種類,建立了一個(gè)工廠來(lái)根據(jù)所需來(lái)創(chuàng)建對(duì)象,這種方式在多態(tài)性編程中是很重要的,允許動(dòng)態(tài)替換類,修改配置等。
/*基本工廠模式代碼*/
<?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(); ?>
工廠模式分為:簡(jiǎn)單工廠模式、工廠方法模式、抽象工廠模式。
簡(jiǎn)單工廠模式,通過(guò)靜態(tài)方法創(chuàng)建對(duì)象。可以理解成,只負(fù)責(zé)生產(chǎn)同一等級(jí)結(jié)構(gòu)中的任何一個(gè)產(chǎn)品,但是不能新增產(chǎn)品。
<?php /** *簡(jiǎn)單工廠模式 * */ 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(); ?>
工廠方法模式,去掉了簡(jiǎn)單工廠模式中方法的靜態(tài)屬性,使其可以被子類集成,定義一個(gè)創(chuàng)建對(duì)象的接口,讓子類去決定實(shí)例化哪個(gè)類??梢岳斫獬桑脕?lái)生產(chǎn)同一等級(jí)結(jié)構(gòu)中的固定產(chǎn)品,但是支持增加產(chǎn)品。
<?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]); ?>
抽象工廠模式,提供一個(gè)創(chuàng)建一系列相關(guān)或者相互依賴的對(duì)象的接口??梢岳斫獬桑脕?lái)生產(chǎn)不用類型的全部產(chǎn)品,但是不能增加新品,支持增加新的類型。
<?php /** * 抽象工廠模式 * */ interface userProperties { function getUsername(); function getGender(); function getJob(); } interface createUser { //將對(duì)象的創(chuàng)建抽象成一個(gè)接口 function createOpen($properties);//內(nèi)向創(chuàng)建 function createIntro($properties);//外向創(chuàng)建 } 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]); ?>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“PHP如何實(shí)現(xiàn)工廠模式”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。