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

yipeiwu_com5年前PHP代码库

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

一、什么是工厂方法模式

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

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

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

三、一般工厂方法模式

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

<?php
/**
*  一般工厂方法设计模式
**/
//工厂抽象类
abstract class Factory
{
  protected abstract function produce();
  public function startFactory()
  {
    $pro = $this->produce();
    return $pro;
  }
}
//文本工厂
class TextFactory extends Factory
{
  protected function produce()
  {
    $textProduct = new TextProduct();
    return $textProduct->getProperties();
  }
}
//图像工厂
class ImageFactory extends Factory
{
  protected function produce()
  {
    $imageProduct = new ImageProduct();
    return $imageProduct->getProperties();
  }
}
//产品类接口
interface Product
{
  public function getProperties();
}
//文本产品
class TextProduct implements Product
{
  private $text;
  function getProperties()
  {
    $this->text = "此处为文本";
    return $this->text;
  }
}
//图像产品
class ImageProduct implements Product
{
  private $image;
  function getProperties()
  {
    $this->image = "此处为图像";
    return $this->image;
  }
}
//客户类
class Client
{
  private $textFactory;
  private $imageFactory;
  public function __construct()
  {
    $this->textFactory = new TextFactory();
    echo $this->textFactory->startFactory() . '<br />';
    $this->imageFactory = new ImageFactory();
    echo $this->imageFactory->startFactory() . '<br />';
  }
}
$client = new Client();
/*运行结果:
此处为文本
此处为图像
*/
?>

四、参数化工厂方法模式

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

<?php
/**
*  参数化工厂方法设计模式
**/
//工厂抽象类
abstract class Factory
{
  protected abstract function produce(Product $product);
  public function startFactory(Product $product)
  {
    $pro = $this->produce($product);
    return $pro;
  }
}
//工厂实现
class ConcreteFactory extends Factory
{
  protected function produce(Product $product)
  {
    return $product->getProperties();
  }
}
//产品类接口
interface Product
{
  public function getProperties();
}
//文本产品
class TextProduct implements Product
{
  private $text;
  public function getProperties()
  {
    $this->text = "此处为文本";
    return $this->text;
  }
}
//图像产品
class ImageProduct implements Product
{
  private $image;
  public function getProperties()
  {
    $this->image = "此处为图像";
    return $this->image;
  }
}
//客户类
class Client
{
  private $factory;
  private $textProduct;
  private $imageProduct;
  public function __construct()
  {
    $factory = new ConcreteFactory();
    $textProduct = new TextProduct();
    $imageProduct = new ImageProduct();
    echo $factory->startFactory($textProduct) . '<br />';
    echo $factory->startFactory($imageProduct) . '<br />';
  }
}
$client = new Client();
/*运行结果:
此处为文本
此处为图像
*/
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php实现快速排序法函数代码

代码1: 复制代码 代码如下: <?php function quicksort($str){ if(count($str)<=1) return $str;//如果个数不大...

php常用字符串查找函数strstr()与strpos()实例分析

本文实例讲述了php常用字符串查找函数strstr()与strpos()。分享给大家供大家参考,具体如下: 一句话使用strpos判断 ===或!==,这样才能达到预期的效果,性能要比s...

PHP输入流php://input实例讲解

对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述。 “php://input allows you to read raw POST data. It i...

PHP实现将优酷土豆腾讯视频html地址转换成flash swf地址的方法

本文实例讲述了PHP实现将优酷土豆腾讯视频html地址转换成flash swf地址的方法。分享给大家供大家参考,具体如下: 很多用户不知道如何复制flash地址,只能在程序中帮他们替换了...

php解析json数据实例

本文以实例形式展示了php解析json数据的方法,这是一个比较实用的功能,分享给大家供大家参考。具体代码如下: <?php $data; $data.= "["; fo...