本文實例講述了PHP設計模式之原型設計模式原理與用法。分享給大家供大家參考,具體如下:
一、什么是原型設計模式
原型設計模式使用一種克隆技術來復制實例化的對象,新對象是通過復制原型實例創建的。原型設計模式的目的是通過使用克隆以減少
實例化對象的開銷。
在原型設計模式中,Client類是不可缺少的一部分。
PHP有一個內置的克隆方法__clone()可以在設計模式中使用,但是不能直接訪問,使用clone關鍵字即可。克隆不會啟動html' target='_blank'>構造函數。
二、什么時候使用原型設計模式
如果一個項目要求你創建某個原型對象的多個實例,就可以使用原型設計模式。
三、原型設計模式實例
這里以現代企業組織為例:
?php* 原型設計模式* 以現代企業組織為例//部門抽象類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- //員工名字設置方法 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 //克隆函數 function __clone() //部門設置函數 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; //工廠部類實例 //構造函數 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
laravel中短信發送驗證碼的實現方法php實例
PHP receiveMail實現收郵件功能php實例
PHP分享圖片的生成方法php技巧
以上就是PHP設計模式之原型設計模式原理與用法分析php技巧的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答