原型设计模式(Prototype Design Pattern)很有意思, 因为它使用了一种克隆技术来复制实例化的对象. 新对象是通过复制原型实例来创建的. 在这里, 实例是批实例化的具体类.原型设计模式的目的是通过使用克隆来减少实例化对象的开销.与其从一个类实例化新对象, 完全可以使用一个已有实例的克隆.
<?php abstract class CloneObject { public $name; public $picture; abstract function __clone(); } class Boy extends CloneObject { public function __construct() { $this->face = "handsome"; $this->name = "chenqionghe"; } public function display() { echo ‘look : ‘.$this->face;; echo ‘<br />‘.$this->name.‘<br />‘; } public function __clone() {} } $boy = new Boy(); $boy->display(); $cloneBoy = clone $boy; $cloneBoy->face = "still handsome"; $cloneBoy->display();
look : handsome chenqionghe look : still handsome chenqionghe
<?php abstract class IPrototype { public $eyeColor; public $winBeat; public $unitEypes; abstract function __clone(); }
<?php include_once(‘IPrototype.php‘); class MaleProto extends IPrototype { const gender = "MALE"; public $mated; public function __construct() { $this->eyeColor = "red"; $this->winBeat = "220"; $this->unitEypes = "760"; } public function __clone(){} }
<?php include_once(‘IPrototype.php‘); class FemaleProto extends IPrototype { const gender = "FEMAIL"; public $fecundity; public function __construct() { $this->eyeColor = "red"; $this->winBeat = "220"; $this->unitEypes = "760"; } public function __clone(){} }
<?php function __autoload($class_name) { include_once $class_name.‘.php‘; } class Client { //用于直接实例化 private $fly1; private $fly2; //用于克隆 private $c1Fly; private $c2Fly; private $updatedCloneFly; public function __construct() { //实例化 $this->fly1 = new MaleProto(); $this->fly2 = new FemaleProto(); //克隆 $this->c1Fly = clone $this->fly1; $this->c2Fly = clone $this->fly2; $this->updatedCloneFly = clone $this->fly2; //更新克隆 $this->c1Fly->mated = "true"; $this->c2Fly->fecundity = ‘186‘; $this->updatedCloneFly->eyeColor = "purple"; $this->updatedCloneFly->winBeat = "220"; $this->updatedCloneFly->unitEyes = ‘750‘; $this->updatedCloneFly->fecundity = ‘92‘; //通过类型提示方法发送 $this->showFly($this->c1Fly); $this->showFly($this->c2Fly); $this->showFly($this->updatedCloneFly); } private function showFly(IPrototype $fly) { echo "Eye color: ".$fly->eyeColor."<br />"; echo "Wing Beats/second: ".$fly->winBeat."<br />"; echo "Eye units: ".$fly->unitEypes."<br />"; $genderNow = $fly::gender; echo "Gender: ".$genderNow."<br />"; if($genderNow == "FEMAIL") { echo "Number of eggs: ".$fly->fecundity."<hr />"; } else { echo "Mated: ".$fly->mated."<hr />"; } } } $worker = new Client();
Eye color: red Wing Beats/second: 220 Eye units: 760 Gender: MALE Mated: trueEye color: red Wing Beats/second: 220 Eye units: 760 Gender: FEMAIL Number of eggs: 186Eye color: purple Wing Beats/second: 220 Eye units: 760 Gender: FEMAIL Number of eggs: 92
<?php abstract class IAcmePrototype { protected $name; protected $id; protected $employeePic; protected $department; //部门 abstract function setDept($orgCode); abstract function getDept(); //名字 public function setName($emName) { $this->name = $emName; } public function getName() { return $this->name; } //ID public function setId($emId) { $this->id = $emId; } public function getId() { return $this->id; } //雇员图像 public function setPic($ePic) { $this->employeePic = $ePic; } public function getPic() { return $this->employeePic; } abstract function __clone(); }
<?php include_once(‘IAcmePrototype.php‘); class Marketing extends IAcmePrototype { const UNIT = "Marketing"; private $sales = "sales"; private $promotion = "promotion"; private $strategic = "strategic planning"; public function setDept($orgCode) { switch($orgCode) { case 101: $this->department = $this->sales; break; case 102: $this->department = $this->promotion; break; case 103: $this->department = $this->strategic; break; default : $this->department = "未识别的市场部"; } } public function getDept() { return $this->department; } public function __clone() {} }
<?php include_once(‘IAcmePrototype.php‘); class Management extends IAcmePrototype { const UNIT = "Management"; private $research = "research"; private $plan = "planning"; private $operations = "operations"; public function setDept($orgCode) { switch($orgCode) { case 201: $this->department = $this->research; break; case 202: $this->department = $this->plan; break; case 203: $this->department = $this->operations; break; default : $this->department = "未识别的管理部"; } } public function getDept() { return $this->department; } public function __clone() {} }
<?php include_once(‘IAcmePrototype.php‘); class Engineering extends IAcmePrototype { const UNIT = "Engineering"; private $development = "development"; private $design = "design"; private $sysAd = "system administration"; public function setDept($orgCode) { switch($orgCode) { case 301: $this->department = $this->development; break; case 302: $this->department = $this->design; break; case 303: $this->department = $this->sysAd; break; default : $this->department = "未识别的工程部"; } } public function getDept() { return $this->department; } public function __clone() {} }
<?php function __autoload($class_name) { include_once $class_name.‘.php‘; } 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‘, ‘tess.png‘); $this->showEmployee($Tess); $Jacob = clone $this->market; $this->setEmployee($Jacob, "Jacob Jones" , 101, ‘jj101-2234‘, ‘jacob.png‘); $this->showEmployee($Jacob); $Ricky = clone $this->manage; $this->setEmployee($Ricky, "Ricky Rodriguez" , 203, ‘rr101-5634‘, ‘ricky.png‘); $this->showEmployee($Ricky); $Olivaia = clone $this->engineer; $this->setEmployee($Olivaia, "Olivaia Perez" , 302, ‘op301-1278‘, ‘olivia.png‘); $this->showEmployee($Olivaia); $John = clone $this->engineer; $this->setEmployee($John, "John Jacson" , 101, ‘jj301-14548‘, ‘john.png‘); $this->showEmployee($John); } private function makeConProto() { $this->market = new Marketing(); $this->manage = new Management(); $this->engineer = new Engineering(); } private function showEmployee(IAcmePrototype $employeeNow) { $px = $employeeNow->getPic(); echo "<img src=$px width=‘150‘ height=‘150‘ /><br />"; echo $employeeNow->getName().‘<br />‘; echo $employeeNow->getDept().‘:‘.$employeeNow::UNIT.‘<br />‘; echo $employeeNow->getId().‘<hr />‘; } private function setEmployee(IAcmePrototype $employeeNow, $nm, $dp, $id, $px) { $employeeNow->setName($nm); $employeeNow->setDept($dp); $employeeNow->setId($id); $employeeNow->setPic($px); } } $worker = new Client();
Tess Smith sales:Marketing ts101-1234 Jacob Jones sales:Marketing jj101-2234 Ricky Rodriguez operations:Management rr101-5634 Olivaia Perez design:Engineering op301-1278 John Jacson 未识别的工程部:Engineering jj301-14548
原文:http://www.cnblogs.com/chenqionghe/p/4772312.html