PHP設計模式之原型設計模式原理與用法分析
本文實例講述了PHP設計模式之原型設計模式原理與用法。分享給大家供大家參考,具體如下:
一、什么是原型設計模式
原型設計模式使用一種克隆技術來復制實例化的對象,新對象是通過復制原型實例創(chuàng)建的。原型設計模式的目的是通過使用克隆以減少
實例化對象的開銷。
在原型設計模式中,Client類是不可缺少的一部分。
PHP有一個內(nèi)置的克隆方法__clone()
可以在設計模式中使用,但是不能直接訪問,使用clone關鍵字即可??寺〔粫訕?gòu)造函數(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; //工廠部類實例 //構(gòu)造函數(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(); ?>
運行結(jié)果:
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)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP+jQuery實現(xiàn)滾屏無刷新動態(tài)加載數(shù)據(jù)功能詳解
這篇文章主要介紹了PHP+jQuery實現(xiàn)滾屏無刷新動態(tài)加載數(shù)據(jù)功能,涉及php動態(tài)讀取數(shù)據(jù)庫及加載數(shù)據(jù)實現(xiàn)滾屏無刷新效果的具體操作技巧,需要的朋友可以參考下2017-05-05php中json_decode()和json_encode()的使用方法
json_decode對JSON格式的字符串進行編碼而json_encode對變量進行 JSON 編碼,需要的朋友可以參考下2012-06-06php array_walk array_map array_filter區(qū)別案例詳解
這篇文章主要介紹了php array_walk array_map array_filter區(qū)別案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09php實現(xiàn)事件監(jiān)聽與觸發(fā)的方法
這篇文章主要介紹了php實現(xiàn)事件監(jiān)聽與觸發(fā)的方法,可實現(xiàn)時間的綁定、觸發(fā)與注銷等功能,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11