当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP设计模式之原型设计模式原理与用法分析

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-12 15:14:38 浏览: 评论:0 

这篇文章主要介绍了PHP设计模式之原型设计模式,简单描述了原型设计模式的概念、原理并结合实例形式分析了php原型设计模式的定义与使用方法,需要的朋友可以参考下

本文实例讲述了PHP设计模式之原型设计模式原理与用法。分享给大家供大家参考,具体如下:

一、什么是原型设计模式

原型设计模式使用一种克隆技术来复制实例化的对象,新对象是通过复制原型实例创建的。原型设计模式的目的是通过使用克隆以减少实例化对象的开销。

在原型设计模式中,Client类是不可缺少的一部分。

PHP有一个内置的克隆方法__clone()可以在设计模式中使用,但是不能直接访问,使用clone关键字即可。克隆不会启动构造函数。

二、什么时候使用原型设计模式

如果一个项目要求你创建某个原型对象的多个实例,就可以使用原型设计模式。

三、原型设计模式实例

这里以现代企业组织为例:

  1. <?php 
  2. /** 
  3. *  原型设计模式 
  4. *        以现代企业组织为例 
  5. **/ 
  6. //部门抽象类 
  7. abstract class IAcmePrototype 
  8. { 
  9.   protected $id;   //员工ID号 
  10.   protected $name;  //员工名字 
  11.   protected $dept;  //员工部门 
  12.   //克隆方法 
  13.   abstract function __clone(); 
  14.   //员工部门设置方法 
  15.   abstract function setDept($orgCode); 
  16.   //员工部门获取方法 
  17.   public function getDept() 
  18.   { 
  19.     return $this->dept; 
  20.   } 
  21.   //员工ID号设置方法 
  22.   public function setId($id) 
  23.   { 
  24.     $this->id = $id; 
  25.   } 
  26.   //员工ID号获取方法 
  27.   public function getId() 
  28.   { 
  29.     return $this->id; 
  30.   } 
  31.   //员工名字设置方法 
  32.   public function setName($name) 
  33.   { 
  34.     $this->name = $name; 
  35.   } 
  36.   //员工名字获取方法 
  37.   public function getName() 
  38.   { 
  39.     return $this->name; 
  40.   } 
  41. } 
  42. //市场部类 
  43. class Marketing extends IAcmePrototype 
  44. { 
  45.   const UNIT = "Marketing";  //标识 
  46.   //市场部门类别 
  47.   private $sales = "sales"; 
  48.   private $promotion = "promotion"; 
  49.   private $strategic = "strategic planning"; 
  50.   //克隆函数 
  51.   function __clone() 
  52.   { 
  53.   } 
  54.   //部门设置函数 
  55.   public function setDept($orgCode) 
  56.   { 
  57.     switch($orgCode) 
  58.     { 
  59.       case 101: 
  60.           $this->dept = $this->sales; 
  61.           break; 
  62.       case 102: 
  63.           $this->dept = $this->promotion; 
  64.           break; 
  65.       case 103: 
  66.           $this->dept = $this->strategic; 
  67.           break; 
  68.       default: 
  69.           $this->dept = "Unrecognized Marketing"; 
  70.     } 
  71.   } 
  72. } 
  73. //管理部类 
  74. class Management extends IAcmePrototype 
  75. { 
  76.   const UNIT = "Management"; 
  77.   private $research = "research"; 
  78.   private $plan = "planning"; 
  79.   private $operations = "operations"; 
  80.   function __clone() 
  81.   { 
  82.   } 
  83.   public function setDept($orgCode) 
  84.   { 
  85.     switch($orgCode) 
  86.     { 
  87.       case 201: 
  88.           $this->dept = $this->research; 
  89.           break; 
  90.       case 202: 
  91.           $this->dept = $this->plan; 
  92.           break; 
  93.       case 203: 
  94.           $this->dept = $this->operations; 
  95.           break; 
  96.       default: 
  97.           $this->dept = "Unrecognized Marketing"; 
  98.     } 
  99.   } 
  100. } 
  101. //工厂部类 
  102. class Engineering extends IAcmePrototype 
  103. { 
  104.   const UNIT = "Engineering"; 
  105.   private $development = "programming"; 
  106.   private $design = "digital artwork"; 
  107.   private $sysAd = "system administration"; 
  108.   function __clone() 
  109.   { 
  110.   } 
  111.   public function setDept($orgCode) 
  112.   { 
  113.     switch($orgCode) 
  114.     { 
  115.       case 301: 
  116.           $this->dept = $this->development; 
  117.           break; 
  118.       case 302: 
  119.           $this->dept = $this->design; 
  120.           break; 
  121.       case 303: 
  122.           $this->dept = $this->sysAd; 
  123.           break; 
  124.       default: 
  125.           $this->dept = "Unrecognized Marketing"; 
  126.     } 
  127.   } 
  128. } 
  129. //客户类 
  130. class Client 
  131. { 
  132.   private $market;  //市场部类实例 
  133.   private $manage;  //管理部类实例 
  134.   private $engineer; //工厂部类实例 
  135.   //构造函数 
  136.   public function __construct() 
  137.   { 
  138.     $this->makeConProto(); 
  139.     //市场部类实例克隆 
  140.     $Tess = clone $this->market; 
  141.     $this->setEmployee($Tess,"Tess Smith",101,"ts101-1234"); 
  142.     $this->showEmployee($Tess); 
  143.     $Jacob = clone $this->market; 
  144.     $this->setEmployee($Jacob,"Jacob Jones",102,"jj101-2234"); 
  145.     $this->showEmployee($Jacob); 
  146.     //管理部类实例克隆 
  147.     $Ricky = clone $this->manage; 
  148.     $this->setEmployee($Ricky,"Ricky Rodrigues",203,"rr203-5634"); 
  149.     $this->showEmployee($Ricky); 
  150.     //工程部类实例克隆 
  151.     $Olivia = clone $this->engineer; 
  152.     $this->setEmployee($Olivia,"Olivia perez",302,"op302-1278"); 
  153.     $this->showEmployee($Olivia); 
  154.     $John = clone $this->engineer; 
  155.     $this->setEmployee($John,"John Jackson",301,"jj301-1454"); 
  156.     $this->showEmployee($John); 
  157.   } 
  158.   //实例化部门对象 
  159.   private function makeConProto() 
  160.   { 
  161.     $this->market = new Marketing(); 
  162.     $this->manage = new Management(); 
  163.     $this->engineer = new Engineering(); 
  164.   } 
  165.   //员工信息设置方法 
  166.   private function setEmployee(IAcmePrototype $employee,$name,$dept,$id) 
  167.   { 
  168.     $employee->setName($name); 
  169.     $employee->setDept($dept); 
  170.     $employee->setId($id); 
  171.   } 
  172.   //员工信息显示方法 
  173.   private function showEmployee(IAcmePrototype $employee) 
  174.   { 
  175.     echo $employee->getName() . '<br />'; 
  176.     echo $employee->getDept() . '<br />'; 
  177.     echo $employee->getId() . '<br />'; 
  178.   } 
  179. } 
  180. $client = new Client(); 
  181. ?> 

运行结果:

  1. Tess Smith 
  2. sales 
  3. ts101-1234 
  4. Jacob Jones 
  5. promotion 
  6. jj101-2234 
  7. Ricky Rodrigues 
  8. operations 
  9. rr203-5634 
  10. Olivia perez 
  11. digital artwork 
  12. op302-1278 
  13. John Jackson 
  14. programming 
  15. jj301-1454 

 

Tags: PHP设计模式 PHP模式原理

分享到: