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

简单工厂不是一个设计模式,只是一种变成习惯,常被误认为是“工厂模式”。(披萨店生产披萨模拟流程)

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

 1 package com.jyu.pizza;
 2 
 3 public abstract class Pizza {
 4 
 5     protected String name;
 6     
 7     public void prepare(){
 8         System.out.println(name+" is preparing...");
 9     }
10     
11     public void bake(){
12         System.out.println(name+" is baking...");
13     }
14     
15     public void cut(){
16         System.out.println(name+" is cutting...");
17     }
18     
19     public void box(){
20         System.out.println(name+" is boxing...");
21     }
22     
23 }
View Code
 1 package com.jyu.pizza;
 2 
 3 
 4 /**
 5  * @author root
 6  *
 7  */
 8 public class CheesePizza extends Pizza {
 9     
10     public CheesePizza(){
11         name = "CheesePizza";
12     }
13 
14 }
View Code
1 package com.jyu.pizza;
2 
3 public class ClamPizza extends Pizza {
4 
5     public ClamPizza() {
6         name = "ClamPizza";
7     }
8 
9 }
View Code
1 package com.jyu.pizza;
2 
3 public class PepperoniPizza extends Pizza {
4 
5     public PepperoniPizza() {
6         name = "PepperoniPizza";
7     }
8 
9 }
View Code
1 package com.jyu.pizza;
2 
3 public class VeggiePizza extends Pizza {
4 
5     public VeggiePizza() {
6         name = "VeggiePizza";
7     }
8 
9 }
 1 package com.jyu.store;
 2 
 3 import com.jyu.factory.SimplePizzaFactory;
 4 import com.jyu.pizza.Pizza;
 5 
 6 /**
 7  * 批萨店
 8  * @author root
 9  *
10  */
11 public class PizzaStore {
12 
13     SimplePizzaFactory factory;
14     
15     public PizzaStore(SimplePizzaFactory factory){
16         this.factory = factory;
17     }
18     
19     public Pizza orderPizza(String type){
20         Pizza pizza;
21         
22         pizza = factory.createPizza(type);
23         
24         pizza.prepare();
25         pizza.bake();
26         pizza.cut();
27         pizza.box();
28         
29         return pizza;
30     }
31 }
 1 package com.jyu.factory;
 2 
 3 import com.jyu.pizza.CheesePizza;
 4 import com.jyu.pizza.ClamPizza;
 5 import com.jyu.pizza.PepperoniPizza;
 6 import com.jyu.pizza.Pizza;
 7 import com.jyu.pizza.VeggiePizza;
 8 
 9 /**
10  * 简单批萨工厂
11  * @author root
12  *
13  */
14 public class SimplePizzaFactory {
15 
16     public Pizza createPizza(String type){
17         Pizza pizza = null;
18         
19         if(type.equals("cheese")){
20             pizza = new CheesePizza();
21         }else if(type.equals("pepperoni")){
22             pizza = new PepperoniPizza();
23         }else if(type.equals("clam")){
24             pizza = new ClamPizza();
25         }else if(type.equals("veggie")){
26             pizza = new VeggiePizza();
27         }
28         return pizza;
29     }
30 }
 1 package com.jyu.test;
 2 
 3 import com.jyu.factory.SimplePizzaFactory;
 4 import com.jyu.store.PizzaStore;
 5 
 6 public class TestSimpleFactory {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         
13         SimplePizzaFactory factory = new SimplePizzaFactory();
14         
15         PizzaStore pizzaStore = new PizzaStore(factory);
16         
17         pizzaStore.orderPizza("cheese");
18     }
19 
20 }
posted @ 2012-09-17 16:50  黄辉杰  阅读(919)  评论(0编辑  收藏  举报