工厂模式

工厂模式专门负责将大量有共同接口的类实例化,工厂模式可以动态地决定将哪一个类实例化不必事先知道每次要实例化哪一个类。工厂模式有以下几种形态

简单工厂模式--静态工厂方法模式static factory method pattern

工厂方法模式--多态性工厂polymorphic factory或虚构造子模式。virtual constructor

抽象工厂模式--工具箱模式kit toolkit

一、简单工厂模式:静态工厂方法模式

  1 /**
  2  *Simple Factory又称静态工厂方法模式
  3  * 简单工厂模式
  4  *以水果为例
  5  */
  6 interface Fruit{
  7 /**
  8  * 包含生长,收获,种植
  9  * 
 10  */
 11     public function grow();
 12    public function harvest();
 13    public function plant();
 14 
 15 }
 16 
 17 
 18 /**
 19  * Apple
 20  */
 21 
 22 class Apple implements Fruit{
 23 
 24     private $treeAge;
 25     
 26     public function grow(){
 27     
 28         echo 'apple is growing';
 29         $this->treeAge=1;
 30     
 31     }
 32     
 33     public function harvest(){
 34         
 35         echo 'apple has been harvest';
 36     }
 37     
 38     public function plant(){
 39 
 40         echo 'apple has been plant';
 41         
 42     }
 43     
 44     /**
 45      * 辅助方法
 46      */
 47     public function getTreeAge(){
 48     
 49         return $this->treeAge;
 50     }
 51 
 52 }
 53 
 54 /**
 55  * grape
 56  */
 57  class Grape implements Fruit{
 58 
 59     private $seedless;
 60     
 61     public function grow(){
 62     
 63         echo 'grape is growing';
 64         $this->seedless='grape';
 65     
 66     }
 67     
 68     public function harvest(){
 69         
 70         echo 'Grape has been harvest';
 71     }
 72     
 73     public function plant(){
 74 
 75         echo 'Grape has been plant';
 76         
 77     }
 78     
 79     /**
 80      * 辅助方法
 81      */
 82     public function getSeedless(){
 83     
 84         return $this->seedless;
 85     }
 86 
 87 }
 88 
 89 /**
 90  * 简单工厂
 91  */
 92 
 93 
 94    class FruitGardener{
 95    
 96        public static function factory($type){
 97        
 98            switch ($type)
 99            {case 'Apple':
100                return new Apple();
101            case 'Graple':
102                return new Grape();
103            default:
104                throw new Exception('We do not have this fruit');
105            };
106     }
107   }
108   
109   
110 $apple =  FruitGardener::factory('Apple');
111 
112 $apple->grow();
View Code

注意:

1、简单工厂模式所创建的对象往往属于一个产品等级结构,这个等级结构可以是MVC模式里的视图VIEW而工厂本身可以是控制器,一个MVC模式可以有一个控制器和多个视图。

如果有多个控制器,则应当考虑使用工厂方法模式

2、以不变应万变。将来的功能扩展时会很困难。只在有限的程度上支持开闭原则。

二、工厂方法模式Factory Method---把产品创建工作留给子类去做,核心类成为了抽象工厂角色,仅仅负责给出具体工厂子类必须实现的接口。而不接触哪一个产品应当被实例化。

 1 /**
 2  *工厂方法模式 
 3  */
 4 interface Creator{
 5 
 6     /**
 7      * 工厂方法
 8      */
 9     public function factory();
10 }
11 
12 interface Product{
13 
14     /**
15      * 产品角色类
16      */
17     
18 }
19 
20 class ConcreteCreator1 implements Creator{
21 
22     /**
23      * 具体的工厂类
24      * 创建产品方法
25      */
26     public function factory(){
27     
28         return new ConcreteProduct1();
29     }
30 
31 }
32 
33 class ConcreteCreator2 implements Creator{
34 
35     /**
36      * 具体的工厂类
37      * 创建产品方法
38      */
39     public function factory(){
40     
41         return new ConcreteProduct2();
42     }
43 
44 }
45 
46 class ConcreteProduct1 implements Product{
47 
48     public function getMyName(){
49     
50     echo 'product1';
51     }
52 
53 }
54 
55 class ConcreteProduct2 implements Product{
56 
57     public function getMyage(){
58     echo '12years old';
59     }
60 
61 }
62 
63 class  client{
64 
65     /**
66      * 客户端
67      * main(some params)
68      */
69     public static  function main(){
70     
71         $creator1=new ConcreteCreator1();
72         $creator2= new ConcreteCreator2();
73         $prod1=$creator1->factory();
74         $prod2=$creator2->factory();
75         $prod1->getMyName();
76         $prod2->getMyage();
77     
78     }
79     
80 
81 
82 }
83 
84 Client::main();
View Code

三、抽象工厂模式--Kit

把产品横纵向分别分成几个系列或等级结构,为了将消费这些产品对象的责任和创建这些产品对象的责任分割开来,引进抽象工厂模式。

  1 /**
  2  * 
  3  * 抽象工厂方法
  4  * @author Lujianchao
  5  *
  6  */
  7 
  8 interface Gardener{};
  9 
 10 class NorthernGardener implements Gardener{
 11 
 12     /**
 13      * 水果的工厂方法
 14      */
 15     public function CreateFruit($name){
 16     
 17         return new NorthernFruit($name);
 18     
 19     }
 20     /**
 21      * 
 22      *蔬菜的工厂方法
 23      */
 24     public function CreateVeggie($name){
 25     
 26         return new NorthernVeggie($name);
 27     }
 28 
 29 }
 30 
 31 class TropicalGardener implements Gardener{
 32 
 33     /**
 34      * 水果的工厂方法
 35      */
 36     public function CreateFruit($name){
 37     
 38         return new TropicalFruit($name);
 39     
 40     }
 41     
 42     /**
 43      * 蔬菜的工厂方法
 44      */
 45     public function CreateVeggie($name){
 46     
 47         return new TropicalVeggie($name);
 48     
 49     }
 50 }
 51 
 52 
 53 /**
 54  * 蔬菜的标志接口
 55  * 
 56  */
 57 
 58 interface Veggie{
 59 
 60 }
 61   
 62 /**
 63  * NortherVeggie
 64  */
 65 
 66 class NorthernVeggie{
 67 
 68     public $name;
 69     
 70     public function __construct($name){
 71     
 72         $this->name=$name;
 73     
 74     }
 75     
 76     
 77     public function getName(){
 78     
 79         return 'NorthernVeg'.$this->name;
 80     }
 81 
 82 
 83 }
 84 
 85 /**
 86  * TropicalVeggie
 87  */
 88 
 89 class TropicalVeggie{
 90 
 91     public $name;
 92     
 93     public function __construct($name){
 94     
 95         $this->name=$name;
 96     
 97     }
 98     
 99     
100     public function getName(){
101     
102         return 'TropicalVeg:'.$this->name;
103     }
104 
105 
106 }
107 
108 
109 /**
110  * 抽象水果
111  */
112 
113    interface Fruit{}
114    
115    /**
116     * 
117     * 北方水果
118     * @author Lujianchao
119     *
120     */
121    
122    class NorthernFruit implements Fruit{
123    
124        private $name;
125        
126        public function __construct($name){
127        
128            $this->name=$name;
129        
130        }
131        
132        public function getName(){
133        
134            return 'NorthernFruit: '.$this->name;
135        }
136         
137    }
138    /**
139     * 
140     * 热带水果
141     *
142     */
143    
144   class TropicalFruit implements Fruit{
145    
146        private $name;
147        
148        public function __construct($name){
149        
150            $this->name=$name;
151        
152        }
153        
154        public function getName(){
155        
156            return 'TropicalFruit: '.$this->name;
157        }
158         
159    }
160 
161 
162 class client{
163 
164     public static function main(){
165     
166         $fruitGa=new NorthernGardener();
167         
168         $northapple=$fruitGa->CreateFruit('pingguo');
169         
170         echo $northapple->getName();
171     
172     }
173 
174 
175 }
176 
177 Client::main();
View Code
原文地址:https://www.cnblogs.com/phplover/p/3086932.html