1,定义抽象产品类
package com.pattern.factory; import java.util.ArrayList; public abstract class Pizza { String name; String dough; String sauce; ArrayList toppings = new ArrayList(); void prepare(){ System.out.println("Prepareing "+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)); } } void bake(){ System.out.println("Bake for 25 minutes at 350"); } void cut(){ System.out.println("Cutting the pizza into diagonal slices"); } void box(){ System.out.println("Place pizza in official PizzaStore box"); } public String getName(){ return name; } }
2,具体产品类
package com.pattern.factory; public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza(){ name = "Chicago Style Deep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Mozzarella Cheese"); } void cut(){ System.out.println("Cutting the pizza into square slices"); } }
package com.pattern.factory; public 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"); } }
3,定义抽象工厂类
package com.pattern.factory; public abstract class PizzaStore { public Pizza orderPizza(String type){ Pizza pizza ; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } protected abstract Pizza createPizza(String type) ; }
4,定义具体工厂类
package com.pattern.factory; import com.pattern.factory.bean.ChicagoStyleClamPizza; import com.pattern.factory.bean.ChicagoStylePepperoniPizza; import com.pattern.factory.bean.ChicagoStyleVeggiePizza; public class ChicagoPizzaStore extends PizzaStore { /** * @see com.pattern.factory.PizzaStore#createPizza(java.lang.String) */ @Override protected Pizza createPizza(String type) { if(type.equals("cheese")){ return new ChicagoStyleCheesePizza(); } else if(type.equals("veggie")){ return new ChicagoStyleVeggiePizza(); } else if(type.equals("clam")){ return new ChicagoStyleClamPizza(); } else if(type.equals("pepperoni")){ return new ChicagoStylePepperoniPizza(); } else { return null; } } }
package com.pattern.factory; import com.pattern.factory.bean.NYStyleClamPizza; import com.pattern.factory.bean.NYStylePepperoniPizza; import com.pattern.factory.bean.NYStyleVeggiePizza; public class NYPizzaStore extends PizzaStore { /** * @see com.pattern.factory.PizzaStore#createPizza(java.lang.String) */ @Override protected Pizza createPizza(String type) { if(type.equals("cheese")){ return new NYStyleCheesePizza(); } else if(type.equals("veggie")){ return new NYStyleVeggiePizza(); } else if(type.equals("clam")){ return new NYStyleClamPizza(); } else if(type.equals("pepperoni")){ return new NYStylePepperoniPizza(); } else { return null; } } }
5,测试类
package com.pattern.factory; 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()+" "); pizza = chicagoStore.orderPizza("cheese"); System.out.println("Joel order a "+ pizza.getName()+" "); } }
测试结果:
Prepareing NY Style Sauce and Cheese Pizza
Tossing dough...
Adding sauce...
Adding toppings:
Grated Reggiano Cheese
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a NY Style Sauce and Cheese Pizza
Prepareing Chicago Style Deep Dish Cheese Pizza
Tossing dough...
Adding sauce...
Adding toppings:
Shredded Mozzarella Cheese
Bake for 25 minutes at 350
Cutting the pizza into square slices
Place pizza in official PizzaStore box
Joel order a Chicago Style Deep Dish Cheese Pizza
类图如下所示: