当前位置:首页 > PHP教程 > php面向对象 > 列表

实例讲解PHP设计模式编程中的简单工厂模式

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-12 10:47:11 浏览: 评论:0 

这篇文章主要介绍了PHP设计模式编程中的简单工厂模式,举了一个水果销售和一个计算器设计的例子,需要的朋友可以参考下。

简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式,简单工厂模式是由一个工厂对象决定创建出那一种产品类的实例。

1.工厂模式的几种形态

工厂模式专门负责将大量有共同接口的类实例化。工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类。工厂模式有以下几种形态:

(1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory Method Pattern)。

(2)工厂方法(Factory Method)模式,又称多态性工厂(Polymorphic Factory)模式或虚拟构造子(Virtual Constructor)模式;

(3)抽象工厂(Abstract Factory)模式,又称工具箱(Kit 或Toolkit)模式。下面就是简单工厂模式的简略类图。

简单工厂模式,或称静态工厂方法模式,是不同的工厂方法模式的一个特殊实现。在其他文献中,简单工厂往往作为普通工厂模式的一个特例讨论。

学习简单工厂模式是对学习工厂方法模式的一个很好的准备,也是对学习其他模式,特别是单例模式和多例模式的一个很好的准备。

2 .简单工厂模式的引进

比如说有一个农场公司,专门向市场销售各类水果。在这个系统里需要描述下列的水果:

葡萄 Grape

草莓 Strawberry

苹果 Apple

水果与其他的植物有很大的不同,就是水果最终是可以采摘食用的。那么一个自然的作法就是建立一个各种水果都适用的接口,以便与农场里的其他植物区分开。如下图所示。

水果接口规定出所有的水果必须实现的接口,包括任何水果类必须具备的方法:种植plant(),生长grow()以及收获harvest()。接口Fruit 的类图如下所示。

这个水果接口的源代码如下所示。

代码清单1:接口Fruit 的源代码

  1. interface Fruit 
  2. { 
  3. public function grow(); 
  4. public function harvest(); 
  5. public function plant(); 
  6. } 

Apple 类是水果类的一种,因此它实现了水果接口所声明的所有方法,另外,由于苹果是多年生植物,因此多出一个treeAge 性质,描述苹果树的树龄,下面是这个苹果类的源代码。

代码清单2:类Apple 的源代码

  1. class Apple implements Fruit 
  2. { 
  3. private $_treeAge; 
  4. public function grow() 
  5. { 
  6. echo "Apple is growing."; 
  7. } 
  8. public function harvest() 
  9. { 
  10. echo "Apple has been harvested."; 
  11. } 
  12. public function plant() 
  13. { 
  14. echo "Apple has been planted."; 
  15. } 
  16. public function getTreeAge() 
  17. { 
  18. return $this->_treeAge; 
  19. } 
  20. public function setTreeAge($treeAge) 
  21. { 
  22. $this->_treeAge = (int) $treeAge; 
  23. } 
  24. } 

同样,Grape 类是水果类的一种,也实现了Fruit 接口所声明的所有的方法。但由于葡萄分有籽和无籽两种,因此,比通常的水果多出一个seedless 性质,如下图所示。

葡萄类的源代码如下所示。可以看出,Grape 类同样实现了水果接口,从而是水果类型的一种子类型。

代码清单3:类Grape 的源代码

  1. class Grape implements Fruit 
  2. { 
  3. private $seedless; 
  4. public function grow() 
  5. { 
  6. echo "Grape is growing."; 
  7. } 
  8. public function harvest() 
  9. { 
  10. echo "Grape has been harvested."; 
  11. } 
  12. public function plant() 
  13. { 
  14. echo "Grape has been planted."; 
  15. } 
  16. public function getSeedless() 
  17. { 
  18. return $this->seedless; 
  19. } 
  20. public function setSeedless($seedless) 
  21. { 
  22. $this->seedless = (boolean) $seedless; 
  23. } 
  24. } 

Strawberry 类实现了Fruit 接口,因此,也是水果类型的子类型,其源代码如下所示。

代码清单4:类Strawberry 的源代码

  1. class Strawberry implements Fruit 
  2. { 
  3. public function grow() 
  4. { 
  5. echo "Strawberry is growing."; 
  6. } 
  7. public function harvest() 
  8. { 
  9. echo "Strawberry has been harvested."; 
  10. } 
  11. public function plant() 
  12. { 
  13. echo "Strawberry has been planted."; 
  14. } 
  15. } 

农场的园丁也是系统的一部分,自然要由一个合适的类来代表,这个类就是FruitGardener 类,其结构由下面的类图描述。

FruitGardener 类会根据客户端的要求,创建出不同的水果对象,比如苹果(Apple),葡萄(Grape)或草莓(Strawberry)的实例。而如果接到不合法的要求,FruitGardener 类会抛出BadFruitException 异常。

园丁类的源代码如下所示。

代码清单5:FruitGardener 类的源代码

  1. class FruitGardener 
  2. { 
  3. public static function factory($which) 
  4. { 
  5. $which = strtolower($which); 
  6. if ($which == 'apple') { 
  7. return new Apple(); 
  8. } elseif ($which == 'strawberry') { 
  9. return new Strawberry(); 
  10. } elseif ($which == 'grape') { 
  11. return new Grape(); 
  12. } else { 
  13. throw new BadFruitException('Bad fruit request'); 
  14. } 
  15. } 
  16. } 

可以看出,园丁类提供了一个静态工厂方法。在客户端的调用下,这个方法创建客户端所需要的水果对象,如果客户端的请求是系统所不支持的,工厂方法就会抛出一个BadFruitException 异常。这个异常类的源代码如下所示。

代码清单6:BadFruitException 类的源代码

  1. class BadFruitException extends Exception 
  2. { 
  3. } 

在使用时,客户端只需调用FruitGardener 的静态方法factory()即可,请见下面的示意性客户端源代码。

代码清单7:怎样使用异常类BadFruitException

  1. try { 
  2. FruitGardener::factory('apple'); 
  3. FruitGardener::factory('grape'); 
  4. FruitGardener::factory('strawberry'); 
  5. //... 
  6. } catch (BadFruitException $e) { 
  7. //... 
  8. } 

这样,农场一定会百果丰收啦!

3.使用简单工厂模式设计一个“面向对象的”计算器

  1. /** 
  2.  * 面向对象计算器 
  3.  * 思路: 
  4.  * 1、面向对象的基本,封装、继承、多太 
  5.  * 2、父类公用类 
  6.  * 3、各种运算类 
  7.  */ 
  8.    
  9. /** 
  10.  * 基类,运算类 
  11.  * 只提供基本数据,不参与运算 
  12.  */ 
  13.    
  14. class Operation { 
  15.     
  16.  // 第一个数 
  17.  public $first_num = 0; 
  18.     
  19.  // 第二个数 
  20.  public $second_num = 0; 
  21.     
  22.  /** 
  23.   * 获取结果,其他类覆盖此方法 
  24.   * @return double $result 
  25.   */ 
  26.  public function getResult() { 
  27.   $result = 0.00; 
  28.      
  29.   return $result; 
  30.  } 
  31. } 
  32.    
  33. /** 
  34.  * 加法类 
  35.  */ 
  36. class OperationAdd extends Operation { 
  37.  /** 
  38.   * 覆盖父类,实现加法算法 
  39.   */ 
  40.  public function getResult() { 
  41.   $result = 0; 
  42.   return $this->first_num + $this->second_num; 
  43.  } 
  44. } 
  45.    
  46. /** 
  47.  * 减法类 
  48.  * 
  49.  */ 
  50. class OperationSub extends Operation { 
  51.  /** 
  52.   * 覆盖父类,实现加法算法 
  53.   */ 
  54.  public function getResult() { 
  55.   $result = 0; 
  56.   return $this->first_num - $this->second_num; 
  57.  } 
  58. } 
  59.    
  60. /** 
  61.  * 乘法类 
  62.  * 
  63.  */ 
  64. class OperationMul extends Operation { 
  65.  /** 
  66.   * 覆盖父类,实现加法算法 
  67.   */ 
  68.  public function getResult() { 
  69.   $result = 0; 
  70.   return $this->first_num * $this->second_num; 
  71.  } 
  72. } 
  73.    
  74. /** 
  75.  * 除类 
  76.  * 
  77.  */ 
  78. class OperationDiv extends Operation { 
  79.  /** 
  80.   * 覆盖父类,实现加法算法 
  81.   */ 
  82.  public function getResult() { 
  83.   $result = 0; 
  84.      
  85.   if ($this->second_num == 0) { 
  86.    throw new Exception('除法操作第二个参数不能为零!'); 
  87.    return 0; 
  88.   } 
  89.      
  90.   return $this->first_num / $this->second_num; 
  91.  } 
  92. } 
  93.    
  94. /** 
  95.  * 工厂类 
  96.  */ 
  97. class OperationFactory { 
  98.  /** 
  99.   * 工厂函数 
  100.   * @param string $operation 
  101.   * @return object 
  102.   */ 
  103.  public function createOperation($operation) { 
  104.   $oper = null; 
  105.      
  106.   switch($operation) { 
  107.    case '+': 
  108.     $oper = new OperationAdd(); 
  109.     break; 
  110.    case '-': 
  111.     $oper = new OperationSub(); 
  112.     break; 
  113.    case '*': 
  114.     $oper = new OperationMul(); 
  115.     break; 
  116.    case '/': 
  117.     $oper = new OperationDiv(); 
  118.     break; 
  119.    default: 
  120.     return 0; 
  121.   } 
  122.   return $oper; 
  123.  } 
  124. } 
  125. $operation = new OperationFactory(); 
  126. $oper = $operation->createOperation('/'); 
  127.    
  128. $oper->first_num = 10; 
  129. $oper->second_num = 20; 
  130. var_dump($oper->getResult());

Tags: PHP设计模式 PHP工厂模式

分享到: