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

PHP设计模式之工厂方法设计模式实例分析

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

本文实例讲述了PHP设计模式之工厂方法设计模式。分享给大家供大家参考,具体如下:

一、什么是工厂方法模式

作为一种创建型设计模式,工厂方法模式就是要创建“某种东西”。对于工厂方法,要创建的“东西”是一个产品,这个产品与创建它的类之间不存在绑定。实际上,为了保持这种松耦合,客户会通过一个工厂发出请求,再由工厂创建所请求的产品。利用工厂方法模式,请求者只发出请求,而不具体创建产品。

二、什么时候使用工厂方法模式

如果实例化对象的子类可能改变,就要使用工厂方法模式。

三、一般工厂方法模式

使用一般工厂方法模式时,客户只包含工厂的引用,一个工厂生产一种产品。增加一种产品的同时需要增加一个新工厂类和一个新产品类。

  1. <?php 
  2. /** 
  3. *  一般工厂方法设计模式 
  4. **/ 
  5. //工厂抽象类 
  6. abstract class Factory 
  7. { 
  8.   protected abstract function produce(); 
  9.   public function startFactory() 
  10.   { 
  11.     $pro = $this->produce(); 
  12.     return $pro; 
  13.   } 
  14. } 
  15. //文本工厂 
  16. class TextFactory extends Factory 
  17. { 
  18.   protected function produce() 
  19.   { 
  20.     $textProduct = new TextProduct(); 
  21.     return $textProduct->getProperties(); 
  22.   } 
  23. } 
  24. //图像工厂 
  25. class ImageFactory extends Factory 
  26. { 
  27.   protected function produce() 
  28.   { 
  29.     $imageProduct = new ImageProduct(); 
  30.     return $imageProduct->getProperties(); 
  31.   } 
  32. } 
  33. //产品类接口 
  34. interface Product 
  35. { 
  36.   public function getProperties(); 
  37. } 
  38. //文本产品 
  39. class TextProduct implements Product 
  40. { 
  41.   private $text; 
  42.   function getProperties() 
  43.   { 
  44.     $this->text = "此处为文本"; 
  45.     return $this->text; 
  46.   } 
  47. } 
  48. //图像产品 
  49. class ImageProduct implements Product 
  50. { 
  51.   private $image; 
  52.   function getProperties() 
  53.   { 
  54.     $this->image = "此处为图像"; 
  55.     return $this->image; 
  56.   } 
  57. } 
  58. //客户类 
  59. class Client 
  60. { 
  61.   private $textFactory; 
  62.   private $imageFactory; 
  63.   public function __construct() 
  64.   { 
  65.     $this->textFactory = new TextFactory(); 
  66.     echo $this->textFactory->startFactory() . '<br />'; 
  67.     $this->imageFactory = new ImageFactory(); 
  68.     echo $this->imageFactory->startFactory() . '<br />'; 
  69.   } 
  70. } 
  71. $client = new Client(); 
  72. /*运行结果: 
  73. 此处为文本 
  74. 此处为图像 
  75. */ 
  76. ?> 

四、参数化工厂方法模式

使用参数化工厂方法模式时,客户包含工厂和产品的引用,发出请求时需要指定产品的种类,一个工厂生产多种产品。增加一种产品时只需要增加一个新产品类即可。

  1. <?php 
  2. /** 
  3. *  参数化工厂方法设计模式 
  4. **/ 
  5. //工厂抽象类 
  6. abstract class Factory 
  7. { 
  8.   protected abstract function produce(Product $product); 
  9.   public function startFactory(Product $product) 
  10.   { 
  11.     $pro = $this->produce($product); 
  12.     return $pro; 
  13.   } 
  14. } 
  15. //工厂实现 
  16. class ConcreteFactory extends Factory 
  17. { 
  18.   protected function produce(Product $product) 
  19.   { 
  20.     return $product->getProperties(); 
  21.   } 
  22. } 
  23. //产品类接口 
  24. interface Product 
  25. { 
  26.   public function getProperties(); 
  27. } 
  28. //文本产品 
  29. class TextProduct implements Product 
  30. { 
  31.   private $text; 
  32.   public function getProperties() 
  33.   { 
  34.     $this->text = "此处为文本"; 
  35.     return $this->text; 
  36.   } 
  37. } 
  38. //图像产品 
  39. class ImageProduct implements Product 
  40. { 
  41.   private $image; 
  42.   public function getProperties() 
  43.   { 
  44.     $this->image = "此处为图像"; 
  45.     return $this->image; 
  46.   } 
  47. } 
  48. //客户类 
  49. class Client 
  50. { 
  51.   private $factory; 
  52.   private $textProduct; 
  53.   private $imageProduct; 
  54.   public function __construct() 
  55.   { 
  56.     $factory = new ConcreteFactory(); 
  57.     $textProduct = new TextProduct(); 
  58.     $imageProduct = new ImageProduct(); 
  59.     echo $factory->startFactory($textProduct) . '<br />'; 
  60.     echo $factory->startFactory($imageProduct) . '<br />'; 
  61.   } 
  62. } 
  63. $client = new Client(); 
  64. /*运行结果: 
  65. 此处为文本 
  66. 此处为图像 
  67. */ 
  68. ?>

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

分享到: