现在的位置: 首页 > 综合 > 正文

PHP实现 工厂模式

2013年11月22日 ⁄ 综合 ⁄ 共 4183字 ⁄ 字号 评论关闭

设计模式-使用php实现工厂方法模式

【概要】
创建型模式
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使用一个类的实例化延迟到其子类【GOF95】

【结构图】

【主要角色】
抽象产品(Product)角色:具体产品对象共有的父类或接口
具体产品(Concrete Product)角色:实现抽象产品角色所定义的接口,并且工厂方法模式所创建的每一个对象都是某具体产品对象的实例
抽象工厂(Creator)角色:模式中任何创建对象的工厂类都要实现这个接口,它声明了工厂方法,该方法返回一个Product类型的对象。
Creator也可以定义一个工厂方法的缺省实现,它返回一个缺省的的ConcreteProduct对象
具体工厂(Concrete Creator)角色:实现抽象工厂接口,具体工厂角色与应用逻辑相关,由应用程序直接调用以创建产品对象。
【优缺点】
优点:工厂方法模式可以允许系统在不修改工厂角色的情况下引进新产品。
缺点:客户可能仅仅为了创建一个特定的ConcreteProduct对象,就不得不创建一个Creator子类
【适用性】
1、当一个类不知道它所必须创建的对象的类的时候
2、当一个类希望由它的子类来指定它所创建的对象的时候
3、当类将创建对象的职责委托给多个帮助子类的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候

【工厂模式php实例】

  1. <?php  
  2.  /** 
  3.  * 工厂方法模式 
  4.  * ------------- 
  5.  * @author      zhaoxuejie <zxj198468@gmail.com> 
  6.  * @package     design pattern  
  7.  * @version     v1.0 2011-12-14 
  8.  */  
  9.   
  10. //抽象产品  
  11. interface Work {  
  12.     public function doWork();   
  13. }  
  14.   
  15. //具体产品实现  
  16. class Student implements Work {  
  17.       
  18.     function doWork(){  
  19.         echo "学生做作业!\n";  
  20.     }  
  21. }  
  22.   
  23. class Teacher implements Work {  
  24.       
  25.     function doWork(){  
  26.         echo "老师批改作业!\n";  
  27.     }  
  28. }  
  29.   
  30. //抽象工厂  
  31. interface WorkerFactory {  
  32.     public function getWorker();  
  33. }  
  34.   
  35. //具体抽象工厂实现  
  36. class StudentFactory {  
  37.       
  38.     function getWorker(){  
  39.         return new Student();  
  40.     }  
  41. }  
  42.   
  43. class TeacherFactory {  
  44.     function getWorker(){  
  45.         return new Teacher();  
  46.     }  
  47. }  
  48.   
  49. //客户端  
  50. class Client {  
  51.       
  52.     static function main() {  
  53.         $s = new Student();  
  54.         $s->doWork();  
  55.           
  56.         $t = new Teacher();  
  57.         $t->doWork();  
  58.     }  
  59. }  
  60.   
  61. Client::main();  
  62. ?>  

【简单工厂模式】
从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
【简单工厂模式php实例】

  1. <?php  
  2.  /** 
  3.  * 简单工厂模式 
  4.  * ------------- 
  5.  * @author      zhaoxuejie <zxj198468@gmail.com> 
  6.  * @package     design pattern  
  7.  * @version     v1.0 2011-12-14 
  8.  */  
  9.   
  10. interface  Comput {  
  11.     public function getResults();     
  12. }  
  13.   
  14. //操作类  
  15. class Operation {  
  16.     protected  $Number_A = 0;  
  17.     protected  $Number_B = 0;  
  18.     protected  $Result = 0;  
  19.       
  20.     //赋值  
  21.     function setNumber($Number_A$Number_B){  
  22.         $this->Number_A = $Number_A;  
  23.         $this->Number_B = $Number_B;  
  24.     }  
  25.       
  26.     //清零  
  27.     function clearResult(){  
  28.         $this->Result = 0;  
  29.     }  
  30. }  
  31.   
  32. //加法  
  33. class OperationAdd extends Operation implements Comput {  
  34.     function getResults(){  
  35.         return $this->Result = ($this->Number_A + $this->Number_B);      
  36.     }  
  37. }  
  38.   
  39. //减法  
  40. class OperationSub extends Operation implements Comput {  
  41.     function getResults(){  
  42.         return $this->Result = ($this->Number_A - $this->Number_B);      
  43.     }  
  44. }  
  45.   
  46. //乘法  
  47. class OperationMul extends Operation implements Comput {  
  48.     function getResults(){  
  49.         return $this->Result = ($this->Number_A * $this->Number_B);      
  50.     }  
  51. }  
  52.   
  53. //除法  
  54. class OperationDiv extends Operation implements Comput {  
  55.     function getResults(){  
  56.         if(intval($this->Number_B) == 0){  
  57.             return $this->Result = 'Error: Division by zero';  
  58.         }  
  59.         return $this->Result = ($this->Number_A / $this->Number_B);      
  60.     }  
  61. }  
  62.   
  63. //工厂  
  64. class OperationFactory {  
  65.     private static $obj;  
  66.       
  67.     public static function CreateOperation($type){  
  68.         try {  
  69.            $error = "Please input the '+', '-', '*', '/' symbols of Math.";  
  70.            switch($type){  
  71.                 case '+' :  
  72.                     self::$obj = new OperationAdd();  
  73.                     break;  
  74.                 case '-' :  
  75.                     self::$obj = new OperationSub();  
  76.                     break;  
  77.                 case '*' :  
  78.                     self::$obj = new OperationMul();  
  79.                     break;  
  80.                 case '/' :  
  81.                     self::$obj = new OperationDiv();  
  82.                     break;  
  83.                 default:  
  84.                     throw new Exception($error);  
  85.             }  
  86.             return self::$obj;  
  87.           
  88.         } catch (Exception $e) {  
  89.             echo 'Caught exception: ',  $e->getMessage(), "\n";  
  90.             exit;  
  91.         }  
  92.     }  
  93. }  
  94.   
  95. //工厂创建实例  
  96. $obj = OperationFactory::CreateOperation('*');  
  97. $obj->setNumber(3, 4);  
  98. echo $obj->getResults();  
  99. ?> 

抱歉!评论已关闭.