工厂模式、抽象工厂

大憨熊 提交于 2019-11-27 20:08:29
  1 /**
  2  * 最初的设计
  3  */
  4 Pizza orderPizaa(String type){
  5     Pizza pizza;
  6     /*
  7      *此代码没有对修改封闭
  8      *压力来自于增加更多的比萨类型
  9      */
 10     if(type.equals("cheese")){
 11         pizza = new CheesePizza();
 12     } else if(type.equals("greek")){
 13         pizza = new GreekPizza();
 14     }  else if(type.equals("pepperoni")){
 15         pizza = new PepperoniPizza();
 16     }
 17 
 18     pizza.prepare();
 19     pizza.bake();
 20     pizza.cut();
 21     pizza.box();
 22     return pizza;
 23 }
 24 /**
 25  *建立一个简单工厂
 26  */
 27 public class SimplePizzaFactory{
 28     public Pizza createPizza(String type){
 29         Pizza pizza = null;
 30 
 31         if(type.equals("cheese")){
 32             pizza = new CheesePizza();
 33         } else if(type.equals("greek")){
 34             pizza = new GreekPizza();
 35         }  else if(type.equals("pepperoni")){
 36             pizza = new PepperoniPizza();
 37         }
 38         return pizza;
 39     }
 40 }
 41 
 42 public class PizzaStore{
 43     SimplePizzaFactory factory;
 44 
 45     public PizzaStore(SimplePizzaFactory factory){
 46         this.factory = factory;
 47     }
 48 
 49     public Pizza orderPizaa(String type){
 50         Pizza pizza;
 51         pizza = fatory.createPizza(type);
 52 
 53         pizza.prepare();
 54         pizza.bake();
 55         pizza.cut();
 56         pizza.box();
 57         return pizza;
 58     }
 59 }
 60 /**
 61  *让比萨制作活动局限于PizzaStore类,而同时又能让这些加盟店依然可以自由地制作该区域的风味
 62  *工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
 63  */
 64 public abstract class PizzaStore{
 65     
 66     public Pizza orderPizza(String type){
 67         Pizza pizza;
 68 
 69         pizza = createPizza(type);
 70         pizza.prepare();
 71         pizza.bake();
 72         pizza.cut();
 73         pizza.box();
 74             
 75         return pizza;
 76     }
 77 
 78     abstract Pizza createPizza(String type);
 79 }
 80     /*
 81      * 开加盟店:纽约风味
 82      */
 83 public class NYPizzaStore extends PizzaStore{
 84 
 85     Pizza createPizza(String item){
 86         if(type.equals("cheese")){
 87             pizza = new NYStyleCheesePizza();
 88         } else if(type.equals("greek")){
 89             pizza = new NYStyleGreekPizza();
 90         } else if(type.equals("pepperoni")){
 91             pizza = new NYStylePepperoniPizza();
 92         } else return null;
 93     }
 94 }
 95 
 96 public abstract class Pizza{
 97     String name;
 98     String dough;
 99     String sauce;
100     ArrayList toppings = new ArrayList();
101     
102     void prepare(){
103         System.out.println("Preparing "+name);
104         ...
105     }
106     void bake(){
107         System.out.println("Bake for 25 minutes at 350");
108     }
109 }
110 
111 public class NYStyleCheesePizza extends Pizza{
112     public NYStyleCheesePizza(){
113         name="NY Style Sauce and Cheese Pizza";
114         dough = "Thin Crust Dough";
115         sauce = "Marinara Sauce";
116 
117         toppings.add("Grated Reggiano Cheese");
118     }
119 }
120 
121 public class PizzaTestDrive{
122     
123     public static void main(String[] args){
124         PizzaStore nyStore = new NYPizzaStore();
125         Pizza pizza = nyStore.orderPizza("cheese");
126     }
127 }
128 
129 /**
130  * 一个很依赖的比萨店
131  */
132 public class DependentPizzaStore{
133     
134     public Pizza createPizza(String style, String type){
135         if(style.equals("NY")){
136             if(type.equals("cheese")){
137                 pizza = new NYStyleCheesePizza();
138             } else if(type.equals("greek")){
139                 pizza = new NYStyleGreekPizza();
140             } else if(type.equals("pepperoni")){
141                 pizza = new NYStylePepperoniPizza();
142             }
143         } else if(style.equals("Chicago")){
144             if(type.equals("cheese")){
145                 pizza = new ChicagoStyleCheesePizza();
146             } else if(type.equals("greek")){
147                 pizza = new ChicagoStyleGreekPizza();
148             } else if(type.equals("pepperoni")){
149                 pizza = new ChicagoStylePepperoniPizza();
150             }
151         } else {
152             System.out.println("Error: invalid type of pizza");
153             return null;
154         }
155 
156         pizza.prepare();
157         pizza.bake();
158         pizza.cut();
159         pizza.box();
160             
161         return pizza;
162     }
163 }
164 /*
165  *设计原则:要依赖对象,不要依赖具体类。
166  */
167 
168 /**
169  * 建造原料工厂
170  */
171 public interface PizzaIngredientFactory{
172     public Dough createDough();
173     public Sauce createSauce();
174     public Cheese createCheese();
175     public Veggies[] createVeggies();
176     public Pepperoni createPepperoni();
177     public Clams createClam();
178 }
179 /**
180  * 创建纽约原料工厂
181  */
182 public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
183     public Dough createDough(){
184         return new ThinCrustDough();
185     }
186     ...
187 }
188 
189 /**
190  * 重做比萨
191  */
192 public abstract class Pizza{
193 
194     String name;
195     Dough dough;
196     Sauce sauce;
197     ...
198 
199     abstract void prepare();
200 
201     void bake(){
202         System.out.println("Bake for 25 minutes at 350");
203     }
204 
205     ...
206 }
207 
208 public class CheesePizza extends Pizza{
209     PizzaIngredientFactory ingredientFactory;
210 
211     public CheesePizza(PizzaIngredientFactory ingredientFactory){
212         this.ingredientFactory = ingredientFactory;
213     }
214 
215     void prepare(){
216         System.out.orintln("Preparing "+name);
217         dough = ingredientFactory.createDough();
218         sauce = ingredientFactory.createSauce();
219         cheese = ingredientFactory.createCheese();
220     }
221 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!