PHP实现 工厂模式

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

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

【结构图】

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

【工厂模式php实例】

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

【简单工厂模式】
从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
【简单工厂模式php实例】
[php] view plain copy print ?
  1.  /** 
  2.  * 简单工厂模式 
  3.  * ------------- 
  4.  * @author      zhaoxuejie  
  5.  * @package     design pattern  
  6.  * @version     v1.0 2011-12-14 
  7.  */  
  8.   
  9. interface  Comput {  
  10.     public function getResults();     
  11. }  
  12.   
  13. //操作类  
  14. class Operation {  
  15.     protected  $Number_A = 0;  
  16.     protected  $Number_B = 0;  
  17.     protected  $Result = 0;  
  18.       
  19.     //赋值  
  20.     function setNumber($Number_A$Number_B){  
  21.         $this->Number_A = $Number_A;  
  22.         $this->Number_B = $Number_B;  
  23.     }  
  24.       
  25.     //清零  
  26.     function clearResult(){  
  27.         $this->Result = 0;  
  28.     }  
  29. }  
  30.   
  31. //加法  
  32. class OperationAdd extends Operation implements Comput {  
  33.     function getResults(){  
  34.         return $this->Result = ($this->Number_A + $this->Number_B);      
  35.     }  
  36. }  
  37.   
  38. //减法  
  39. class OperationSub extends Operation implements Comput {  
  40.     function getResults(){  
  41.         return $this->Result = ($this->Number_A - $this->Number_B);      
  42.     }  
  43. }  
  44.   
  45. //乘法  
  46. class OperationMul extends Operation implements Comput {  
  47.     function getResults(){  
  48.         return $this->Result = ($this->Number_A * $this->Number_B);      
  49.     }  
  50. }  
  51.   
  52. //除法  
  53. class OperationDiv extends Operation implements Comput {  
  54.     function getResults(){  
  55.         if(intval($this->Number_B) == 0){  
  56.             return $this->Result = 'Error: Division by zero';  
  57.         }  
  58.         return $this->Result = ($this->Number_A / $this->Number_B);      
  59.     }  
  60. }  
  61.   
  62. //工厂  
  63. class OperationFactory {  
  64.     private static $obj;  
  65.       
  66.     public static function CreateOperation($type){  
  67.         try {  
  68.            $error = "Please input the '+', '-', '*', '/' symbols of Math.";  
  69.            switch($type){  
  70.                 case '+' :  
  71.                     self::$obj = new OperationAdd();  
  72.                     break;  
  73.                 case '-' :  
  74.                     self::$obj = new OperationSub();  
  75.                     break;  
  76.                 case '*' :  
  77.                     self::$obj = new OperationMul();  
  78.                     break;  
  79.                 case '/' :  
  80.                     self::$obj = new OperationDiv();  
  81.                     break;  
  82.                 default:  
  83.                     throw new Exception($error);  
  84.             }  
  85.             return self::$obj;  
  86.           
  87.         } catch (Exception $e) {  
  88.             echo 'Caught exception: ',  $e->getMessage(), "\n";  
  89.             exit;  
  90.         }  
  91.     }  
  92. }  
  93.   
  94. //工厂创建实例  
  95. $obj = OperationFactory::CreateOperation('*');  
  96. $obj->setNumber(3, 4);  
  97. echo $obj->getResults();  
  98. ?> 

你可能感兴趣的:(php)