這篇文章主要介紹了PHP設計模式之原型設計模式,簡單描述了原型設計模式的概念、原理並結合執行個體形式分析了php原型設計模式的定義與使用方法,需要的朋友可以參考下
本文執行個體講述了PHP設計模式之原型設計模式原理與用法。分享給大家供大家參考,具體如下:
一、什麼是原型設計模式
原型設計模式使用一種複製技術來複製執行個體化的對象,新對象是通過複製原型執行個體建立的。原型設計模式的目的是通過使用複製以減少
執行個體化對象的開銷。
在原型設計模式中,Client類是不可缺少的一部分。
PHP有一個內建的複製方法__clone()
可以在設計模式中使用,但是不能直接存取,使用clone關鍵字即可。複製不會啟動建構函式。
二、什麼時候使用原型設計模式
如果一個項目要求你建立某個原型對象的多個執行個體,就可以使用原型設計模式。
三、原型設計模式執行個體
這裡以現代企業組織為例:
<?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->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"; //複製函數 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技巧