您好,登錄后才能下訂單哦!
這篇文章主要介紹PHP設計模式之原型設計模式的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
一、什么是原型設計模式
原型設計模式使用一種克隆技術來復制實例化的對象,新對象是通過復制原型實例創(chuàng)建的。原型設計模式的目的是通過使用克隆以減少
實例化對象的開銷。
在原型設計模式中,Client類是不可缺少的一部分。
PHP有一個內(nèi)置的克隆方法__clone()
可以在設計模式中使用,但是不能直接訪問,使用clone關鍵字即可。克隆不會啟動構造函數(shù)。
二、什么時候使用原型設計模式
如果一個項目要求你創(chuàng)建某個原型對象的多個實例,就可以使用原型設計模式。
三、原型設計模式實例
這里以現(xiàn)代企業(yè)組織為例:
<?php /** * 原型設計模式 * 以現(xiàn)代企業(yè)組織為例 **/ //部門抽象類 abstract class IAcmePrototype { protected $id; //員工ID號 protected $name; //員工名字 protected $dept; //員工部門 //克隆方法 abstract function __clone(); //員工部門設置方法 abstract function setDept($orgCode); //員工部門獲取方法 public function getDept() { return $this->dept; } //員工ID號設置方法 public function setId($id) { $this->id = $id; } //員工ID號獲取方法 public function getId() { return $this->id; } //員工名字設置方法 public function setName($name) { $this->name = $name; } //員工名字獲取方法 public function getName() { return $this->name; } } //市場部類 class Marketing extends IAcmePrototype { const UNIT = "Marketing"; //標識 //市場部門類別 private $sales = "sales"; private $promotion = "promotion"; private $strategic = "strategic planning"; //克隆函數(shù) function __clone() { } //部門設置函數(shù) public function setDept($orgCode) { switch($orgCode) { case 101: $this->dept = $this->sales; break; case 102: $this->dept = $this->promotion; break; case 103: $this->dept = $this->strategic; break; default: $this->dept = "Unrecognized Marketing"; } } } //管理部類 class Management extends IAcmePrototype { const UNIT = "Management"; private $research = "research"; private $plan = "planning"; private $operations = "operations"; function __clone() { } public function setDept($orgCode) { switch($orgCode) { case 201: $this->dept = $this->research; break; case 202: $this->dept = $this->plan; break; case 203: $this->dept = $this->operations; break; default: $this->dept = "Unrecognized Marketing"; } } } //工廠部類 class Engineering extends IAcmePrototype { const UNIT = "Engineering"; private $development = "programming"; private $design = "digital artwork"; private $sysAd = "system administration"; function __clone() { } public function setDept($orgCode) { switch($orgCode) { case 301: $this->dept = $this->development; break; case 302: $this->dept = $this->design; break; case 303: $this->dept = $this->sysAd; break; default: $this->dept = "Unrecognized Marketing"; } } } //客戶類 class Client { private $market; //市場部類實例 private $manage; //管理部類實例 private $engineer; //工廠部類實例 //構造函數(shù) public function __construct() { $this->makeConProto(); //市場部類實例克隆 $Tess = clone $this->market; $this->setEmployee($Tess,"Tess Smith",101,"ts101-1234"); $this->showEmployee($Tess); $Jacob = clone $this->market; $this->setEmployee($Jacob,"Jacob Jones",102,"jj101-2234"); $this->showEmployee($Jacob); //管理部類實例克隆 $Ricky = clone $this->manage; $this->setEmployee($Ricky,"Ricky Rodrigues",203,"rr203-5634"); $this->showEmployee($Ricky); //工程部類實例克隆 $Olivia = clone $this->engineer; $this->setEmployee($Olivia,"Olivia perez",302,"op302-1278"); $this->showEmployee($Olivia); $John = clone $this->engineer; $this->setEmployee($John,"John Jackson",301,"jj301-1454"); $this->showEmployee($John); } //實例化部門對象 private function makeConProto() { $this->market = new Marketing(); $this->manage = new Management(); $this->engineer = new Engineering(); } //員工信息設置方法 private function setEmployee(IAcmePrototype $employee,$name,$dept,$id) { $employee->setName($name); $employee->setDept($dept); $employee->setId($id); } //員工信息顯示方法 private function showEmployee(IAcmePrototype $employee) { echo $employee->getName() . '<br />'; echo $employee->getDept() . '<br />'; echo $employee->getId() . '<br />'; } } $client = new Client(); ?>
運行結果:
Tess Smith
sales
ts101-1234
Jacob Jones
promotion
jj101-2234
Ricky Rodrigues
operations
rr203-5634
Olivia perez
digital artwork
op302-1278
John Jackson
programming
jj301-1454
以上是“PHP設計模式之原型設計模式的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。