当前位置:首页 > 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. public function grow(); 
  3. public function harvest(); 
  4. public function plant(); 

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

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

  1. class Apple implements Fruit 
  2. private $_treeAge
  3. public function grow() 
  4. echo "Apple is growing."
  5. public function harvest() 
  6. echo "Apple has been harvested."
  7. public function plant() 
  8. echo "Apple has been planted."
  9. public function getTreeAge() 
  10. return $this->_treeAge; 
  11. public function setTreeAge($treeAge
  12. $this->_treeAge = (int) $treeAge

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

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

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

  1. class Grape implements Fruit 
  2. private $seedless
  3. public function grow() 
  4. echo "Grape is growing."
  5. public function harvest() 
  6. echo "Grape has been harvested."
  7. public function plant() 
  8. echo "Grape has been planted."
  9. public function getSeedless() 
  10. return $this->seedless; 
  11. public function setSeedless($seedless
  12. $this->seedless = (boolean) $seedless

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

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

  1. class Strawberry implements Fruit 
  2. public function grow() 
  3. echo "Strawberry is growing."
  4. public function harvest() 
  5. echo "Strawberry has been harvested."
  6. public function plant() 
  7. echo "Strawberry has been planted."

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

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

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

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

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

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

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

  1. class BadFruitException extends Exception 

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

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

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

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

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. class OperationAdd extends Operation { 
  36.  /** 
  37.   * 覆盖父类,实现加法算法 
  38.   */ 
  39.  public function getResult() { 
  40.   $result = 0; 
  41.   return $this->first_num + $this->second_num; 
  42.  } 
  43.    
  44. /** 
  45.  * 减法类 
  46.  * 
  47.  */ 
  48. class OperationSub extends Operation { 
  49.  /** 
  50.   * 覆盖父类,实现加法算法 
  51.   */ 
  52.  public function getResult() { 
  53.   $result = 0; 
  54.   return $this->first_num - $this->second_num; 
  55.  } 
  56.    
  57. /** 
  58.  * 乘法类 
  59.  * 
  60.  */ 
  61. class OperationMul extends Operation { 
  62.  /** 
  63.   * 覆盖父类,实现加法算法 
  64.   */ 
  65.  public function getResult() { 
  66.   $result = 0; 
  67.   return $this->first_num * $this->second_num; 
  68.  } 
  69.    
  70. /** 
  71.  * 除类 
  72.  * 
  73.  */ 
  74. class OperationDiv extends Operation { 
  75.  /** 
  76.   * 覆盖父类,实现加法算法 
  77.   */ 
  78.  public function getResult() { 
  79.   $result = 0; 
  80.      
  81.   if ($this->second_num == 0) { 
  82.    throw new Exception('除法操作第二个参数不能为零!'); 
  83.    return 0; 
  84.   } 
  85.      
  86.   return $this->first_num / $this->second_num; 
  87.  } 
  88.    
  89. /** 
  90.  * 工厂类 
  91.  */ 
  92. class OperationFactory { 
  93.  /** 
  94.   * 工厂函数 
  95.   * @param string $operation 
  96.   * @return object 
  97.   */ 
  98.  public function createOperation($operation) { 
  99.   $oper = null; 
  100.      
  101.   switch($operation) { 
  102.    case '+'
  103.     $oper = new OperationAdd(); 
  104.     break
  105.    case '-'
  106.     $oper = new OperationSub(); 
  107.     break
  108.    case '*'
  109.     $oper = new OperationMul(); 
  110.     break
  111.    case '/'
  112.     $oper = new OperationDiv(); 
  113.     break
  114.    default
  115.     return 0; 
  116.   } 
  117.   return $oper
  118.  } 
  119. $operation = new OperationFactory(); 
  120. $oper = $operation->createOperation('/'); 
  121.    
  122. $oper->first_num = 10; 
  123. $oper->second_num = 20; 
  124. var_dump($oper->getResult());

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

分享到: