设计模式之四:抽象工厂(披萨店生产披萨模拟流程)

抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。(披萨店生产披萨模拟流程)

抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道(或关心)实际产出的具体产品是什么。这样一来就从具体的产品中被解藕。

依赖倒置原则:要依赖抽象,不要依赖具体类。

这个原则说明了:不能让高层组件依赖底层组件,而且,不管高层或底层组件,“两者”都应该依赖抽象。

工程名称:AbstractFactory 下载目录:http://www.cnblogs.com/jrsmith/admin/Files.aspx ,AbstractFactory.zip

 1 package com.jyu.factory;
 2 
 3 import com.jyu.material.Dough;
 4 import com.jyu.material.Sauce;
 5 
 6 /**
 7  * 定义抽象原料工厂
 8  * 没有原料都有一个对应的方法创建该原料
 9  * @author root
10  *
11  */
12 public interface PizzaIngredientFactory {
13 
14     public Dough createDough();
15     public Sauce createSauce();
16     /*public Cheese createCheese();
17     public Veggies[] createVeggies();
18     public Pepperoni createPepperoni();
19     public Clams createClam();*/
20 }
View Code
 1 package com.jyu.factory;
 2 
 3 import com.jyu.material.Dough;
 4 import com.jyu.material.MarinaraSauce;
 5 import com.jyu.material.Sauce;
 6 import com.jyu.material.ThinDough;
 7 
 8 /**
 9  * 纽约原料工厂
10  * @author root
11  *
12  */
13 public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
14 
15     @Override
16     public Dough createDough() {
17         return new ThinDough();
18     }
19 
20     @Override
21     public Sauce createSauce() {
22         return new MarinaraSauce();
23     }
24 
25 }
1 package com.jyu.material;
2 
3 public abstract class Dough {
4 
5 }
1 package com.jyu.material;
2 
3 public abstract class Sauce {
4 
5 }
View Code
1 package com.jyu.material;
2 
3 public class ThinDough extends Dough {
4 
5     public ThinDough(){
6         System.out.println("ThinDough material is going to be got ready....done");
7     }
8 }
View Code
1 package com.jyu.material;
2 
3 public class MarinaraSauce extends Sauce {
4 
5     public MarinaraSauce(){
6         System.out.println("MarinaraSauce is going to be got ready...done");
7     }
8 }
 1 package com.jyu.pizza;
 2 
 3 import com.jyu.material.Dough;
 4 import com.jyu.material.Sauce;
 5 
 6 /**
 7  *抽象批萨类
 8  * @author root
 9  *
10  */
11 public abstract class Pizza {
12 
13     public String name;
14     public Dough dough;
15     public Sauce sauce;
16     
17     public abstract void prepare();
18     
19     public void bake(){
20         System.out.println("Bake for 25 minutes at 350");
21     }
22     
23     public void cut(){
24         System.out.println("Cutting the pizza into diagonal slices");
25     }
26     
27     public void box(){
28         System.out.println("Place pizza in offical PizzaStore box");
29     }
30 
31     public String getName() {
32         return name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38     
39     public String toStr(){
40         //这里是打印批萨的代码
41         return null;
42     }
43 }
View Code
 1 /**
 2  * 
 3  */
 4 package com.jyu.pizza;
 5 
 6 import com.jyu.factory.PizzaIngredientFactory;
 7 
 8 /**
 9  * @author root
10  *
11  */
12 public class CheesePizza extends Pizza {
13 
14     PizzaIngredientFactory ingredientFactory;
15     
16     public CheesePizza(PizzaIngredientFactory ingredientFactory) {
17         this.ingredientFactory = ingredientFactory;
18     }
19 
20     @Override
21     public void prepare() {
22 
23         System.out.println("Preparing " + name);
24         dough = ingredientFactory.createDough();
25         sauce = ingredientFactory.createSauce();
26     }
27 
28 }
 1 package com.jyu.store;
 2 
 3 import com.jyu.pizza.Pizza;
 4 
 5 /**
 6  * 抽象 批萨店 类
 7  * @author root
 8  *
 9  */
10 public abstract class PizzaStore {
11 
12     public Pizza orderPizza(String type){
13         Pizza pizza;
14         
15         pizza = createPizza(type);
16         
17         pizza.prepare();
18         pizza.bake();
19         pizza.cut();
20         pizza.box();
21         
22         return pizza;
23     }
24     
25     /**
26      * 生产具体类型批萨工厂
27      * @param type
28      * @return
29      */
30     public abstract Pizza createPizza(String type);
31 }
View Code
 1 /**
 2  * 
 3  */
 4 package com.jyu.store;
 5 
 6 import com.jyu.factory.NYPizzaIngredientFactory;
 7 import com.jyu.factory.PizzaIngredientFactory;
 8 import com.jyu.pizza.CheesePizza;
 9 import com.jyu.pizza.Pizza;
10 
11 /**
12  * @author root
13  *
14  */
15 public class NYPizzaStore extends PizzaStore {
16 
17     @Override
18     public Pizza createPizza(String type) {
19 
20         Pizza pizza = null;
21         PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();
22         
23         if(type.equals("cheese")){
24             pizza = new CheesePizza(ingredientFactory);
25             pizza.setName("New York Style Cheese Pizza");
26         }
27         return pizza;
28     }
29 
30 }
 1 package com.jyu.test;
 2 
 3 import com.jyu.store.PizzaStore;
 4 
 5 public class TestAbstractFactory {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11 
12         PizzaStore NYPizzaStore = new com.jyu.store.NYPizzaStore();
13         NYPizzaStore.orderPizza("cheese");
14     }
15 
16 }
posted @ 2012-09-18 10:35  黄辉杰  阅读(1250)  评论(0编辑  收藏  举报