PHP设计模式三:原型设计模式
一、什么是原型设计模式
原型设计模式使用一种克隆技术来复制实例化的对象,新对象是通过复制原型实例创建的。原型设计模式的目的是通过使用克隆以减少
实例化对象的开销。
在原型设计模式中,Client类是不可缺少的一部分。
PHP有一个内置的克隆方法__clone()可以在设计模式中使用,但是不能直接访问,使用clone关键字即可。克隆不会启动构造函数。
二、什么时候使用原型设计模式
如果一个项目要求你创建某个原型对象的多个实例,就可以使用原型设计模式。
三、原型设计模式实例
这里以现代企业组织为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | <?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() . ": " . $employee ::UNIT . '<br />' ; echo $employee ->getId() . '<br />' ; } } $client = new Client(); ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)