浏览 1878 次
锁定老帖子 主题:工厂方法 读书笔记
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-03
package pattern; import java.util.ArrayList; public abstract class PizzaStore { SimplePizzaFactory factory; public PizzaStore() { } public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } abstract Pizza createPizza(String type); } class NYPizzaStore extends PizzaStore { Pizza createPizza(String item) { if (item.equals("cheese")) { return new NYStyleCheesePizza(); } else if (item.equals("pepperoni")) { return new NYStylePepperoniPizza(); } else if (item.equals("clam")) { return new NYStyleClamPizza(); } else if (item.equals("veggie")) { return new NYStyleVeggiePizza(); } else return null; } } class ChicagoPizzaStore extends PizzaStore { Pizza createPizza(String item) { if (item.equals("cheese")) { return new ChicagoStyleCheesePizza(); } else if (item.equals("pepperoni")) { return new ChicagoStylePepperoniPizza(); } else if (item.equals("clam")) { return new ChicagoStyleClamPizz(); } else if (item.equals("veggie")) { return new ChicagoStyleVeggiePizza(); } else return null; } } class CaliforniaPizzaStore extends PizzaStore { Pizza createPizza(String item) { if (item.equals("cheese")) { return new CaliforniaStyleCheesePizza(); } else if (item.equals("pepperoni")) { return new CaliforniaStylePepperoniPizza(); } else if (item.equals("clam")) { return new CaliforniaStyleClamPizza(); } else if (item.equals("veggie")) { return new CaliforniaStyleVeggiePizza(); } else return null; } } abstract class Pizza { String name; String dough; String sauce; ArrayList toppings = new ArrayList(); public void prepare() { System.out.println("Preparing" + name); System.out.println("Tossing dough..."); System.out.println("Adding sauce..."); System.out.println("Adding toppings: "); for (int i = 0; i < toppings.size(); i++) { System.out.println(" " + toppings.get(i)); } } public void bake() { System.out.println("Bake for 25 minutes at 350"); } public void cut() { System.out.println("Cutting the pizza into diagonal slices"); } public void box() { System.out.println("Place pizza in official PizzaStore box"); } public String getName() { return name; } } class CheesePizza extends Pizza { public CheesePizza() { System.out.println("Cheesepizza"); } } class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = "NY Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Reggiano Cheese"); } } class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() { name = "Chicago StyleDeep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Mozzarella Cheese"); } public void cut() { System.out.println("Cutting the pizza into square slices"); } } class CaliforniaStyleCheesePizza extends Pizza { public CaliforniaStyleCheesePizza() { name = "California Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated California Cheese"); } } class PepperoniPizza extends Pizza { public PepperoniPizza() { System.out.println("PepperoniPizza"); } } class NYStylePepperoniPizza extends Pizza { public NYStylePepperoniPizza() { name = "NY Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Pepperoni Cheese"); } } class ChicagoStylePepperoniPizza extends Pizza { public ChicagoStylePepperoniPizza() { name = "Chicago StyleDeep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Pepperoni Cheese"); } public void cut() { System.out.println("Cutting the pizza into square slices"); } } class CaliforniaStylePepperoniPizza extends Pizza { public CaliforniaStylePepperoniPizza() { name = "California Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Pepperoni Cheese"); } } class ClamPizza extends Pizza { public ClamPizza() { System.out.println("ClamPizza"); } } class NYStyleClamPizza extends Pizza { public NYStyleClamPizza() { name = "NY Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated ClamPizza Cheese"); } } class ChicagoStyleClamPizz extends Pizza { public ChicagoStyleClamPizz() { name = "Chicago StyleDeep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Clam Cheese"); } public void cut() { System.out.println("Cutting the pizza into square slices"); } } class CaliforniaStyleClamPizza extends Pizza { public CaliforniaStyleClamPizza() { name = "California Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Clam Cheese"); } } class VeggiePizza extends Pizza { public VeggiePizza() { System.out.println("VeggiePizza"); } } class NYStyleVeggiePizza extends Pizza { public NYStyleVeggiePizza() { name = "NY Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated VeggiePizza Cheese"); } } class ChicagoStyleVeggiePizza extends Pizza { public ChicagoStyleVeggiePizza() { name = "Chicago StyleDeep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Veggie Cheese"); } public void cut() { System.out.println("Cutting the pizza into square slices"); } } class CaliforniaStyleVeggiePizza extends Pizza { public CaliforniaStyleVeggiePizza() { name = "California Style sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Veggie Cheese"); } } public class PizzaTestDrive { public static void main(String[] args) { PizzaStore nyStore = new NYPizzaStore(); PizzaStore chicagoStore = new ChicagoPizzaStore(); Pizza pizza = nyStore.orderPizza("cheese"); System.out.println("Ethan ordered a " + pizza.getName() + "\n"); pizza = chicagoStore.orderPizza("cheese"); System.out.println("Joel ordered a " + pizza.getName() + "\n"); } } public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("cheese")) { pizza = new CheesePizza(); }else if(type.equals("pepperoni")){ pizza=new PepperoniPizza(); }else if(type.equals("clam")){ pizza=new ClamPizza(); }else if(type.equals("veggie")){ pizza=new VeggiePizza(); } return pizza; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |