抽象工廠模式提供了一個接口,用於創建相關或依賴對象的家族,而不需要明確指定具體類。
類圖
(圖片源於網絡)
代碼實現(Java)
由於代碼較多,不在此展示,而存於附件。
測試代碼
// PizzaTestDrive.java 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 + "\n"); pizza = chicagoStore.orderPizza("cheese"); System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("clam"); System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("clam"); System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("pepperoni"); System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("pepperoni"); System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("veggie"); System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("veggie"); System.out.println("Joel ordered a " + pizza + "\n"); } }
運行效果
--- Making a New York Style Cheese Pizza --- Preparing New York Style Cheese Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Ethan ordered a ---- New York Style Cheese Pizza ---- Thin Crust Dough Marinara Sauce Reggiano Cheese --- Making a Chicago Style Cheese Pizza --- Preparing Chicago Style Cheese Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Joel ordered a ---- Chicago Style Cheese Pizza ---- ThickCrust style extra thick crust dough Tomato sauce with plum tomatoes Shredded Mozzarella --- Making a New York Style Clam Pizza --- Preparing New York Style Clam Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Ethan ordered a ---- New York Style Clam Pizza ---- Thin Crust Dough Marinara Sauce Reggiano Cheese Fresh Clams from Long Island Sound --- Making a Chicago Style Clam Pizza --- Preparing Chicago Style Clam Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Joel ordered a ---- Chicago Style Clam Pizza ---- ThickCrust style extra thick crust dough Tomato sauce with plum tomatoes Shredded Mozzarella Frozen Clams from Chesapeake Bay --- Making a New York Style Pepperoni Pizza --- Preparing New York Style Pepperoni Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Ethan ordered a ---- New York Style Pepperoni Pizza ---- Thin Crust Dough Marinara Sauce Reggiano Cheese Garlic, Onion, Mushrooms, Red Pepper Sliced Pepperoni --- Making a Chicago Style Pepperoni Pizza --- Preparing Chicago Style Pepperoni Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Joel ordered a ---- Chicago Style Pepperoni Pizza ---- ThickCrust style extra thick crust dough Tomato sauce with plum tomatoes Shredded Mozzarella Black Olives, Spinach, Eggplant Sliced Pepperoni --- Making a New York Style Veggie Pizza --- Preparing New York Style Veggie Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Ethan ordered a ---- New York Style Veggie Pizza ---- Thin Crust Dough Marinara Sauce Reggiano Cheese Garlic, Onion, Mushrooms, Red Pepper --- Making a Chicago Style Veggie Pizza --- Preparing Chicago Style Veggie Pizza Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box Joel ordered a ---- Chicago Style Veggie Pizza ---- ThickCrust style extra thick crust dough Tomato sauce with plum tomatoes Shredded Mozzarella Black Olives, Spinach, Eggplant |