• 【设计模式】Headfirst设计模式——工厂运行代码


    import java.util.ArrayList;
    
    public class Pizza {
        String name;
        String dough;
        String sauce;
        ArrayList toppings = new ArrayList();
    
        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));
            }
        }
        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;
        }
    
        public String toString() {
            StringBuffer display = new StringBuffer();
            display.append("---- " + name + " ----
    ");
            display.append(dough + "
    ");
            display.append(sauce + "
    ");
            for (int i = 0; i < toppings.size(); i++) {
                display.append((String )toppings.get(i) + "
    ");
            }
            return display.toString();
        }
    
        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 ordered a " + pizza.getName() + "
    ");
    
            pizza = chicagoStore.orderPizza("clam");
            System.out.println("Joel ordered a " + pizza.getName() + "
    ");
    
            pizza = chicagoStore.orderPizza("pepperoni");
            System.out.println("Joel ordered a " + pizza.getName() + "
    ");
    
            pizza = chicagoStore.orderPizza("veggie");
            System.out.println("Joel ordered a " + pizza.getName() + "
    ");
        }
    }
    abstract class PizzaStore {
    
        abstract Pizza createPizza(String item);
    
        public Pizza orderPizza(String type) {
            Pizza pizza = createPizza(type);
            System.out.println("--- Making a " + pizza.getName() + " ---");
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
            return pizza;
        }
    }
    class DependentPizzaStore {
    
        public Pizza createPizza(String style, String type) {
            Pizza pizza = null;
            if (style.equals("NY")) {
                if (type.equals("cheese")) {
                    pizza = new NYStyleCheesePizza();}
            } else if (style.equals("Chicago")) {
                if (type.equals("cheese")) {
                    pizza = new ChicagoStyleCheesePizza();
                } else if (type.equals("veggie")) {
                    pizza = new ChicagoStyleVeggiePizza();
                } else if (type.equals("clam")) {
                    pizza = new ChicagoStyleClamPizza();
                } else if (type.equals("pepperoni")) {
                    pizza = new ChicagoStylePepperoniPizza();
                }
            } else {
                System.out.println("Error: invalid type of pizza");
                return null;
            }
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
            return pizza;
        }
    }
    class ChicagoPizzaStore extends PizzaStore {
    
        Pizza createPizza(String item) {
            if (item.equals("cheese")) {
                return new ChicagoStyleCheesePizza();
            } else if (item.equals("veggie")) {
                return new ChicagoStyleVeggiePizza();
            } else if (item.equals("clam")) {
                return new ChicagoStyleClamPizza();
            } else if (item.equals("pepperoni")) {
                return new ChicagoStylePepperoniPizza();
            } else return null;
        }
    }
    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");
        }
    }
    class ChicagoStyleClamPizza extends Pizza {
        public ChicagoStyleClamPizza() {
            name = "Chicago Style Clam Pizza";
            dough = "Extra Thick Crust Dough";
            sauce = "Plum Tomato Sauce";
    
            toppings.add("Shredded Mozzarella Cheese");
            toppings.add("Frozen Clams from Chesapeake Bay");
        }
    
        void cut() {
            System.out.println("Cutting the pizza into square slices");
        }
    }
    class ChicagoStylePepperoniPizza extends Pizza {
        public ChicagoStylePepperoniPizza() {
            name = "Chicago Style Pepperoni Pizza";
            dough = "Extra Thick Crust Dough";
            sauce = "Plum Tomato Sauce";
    
            toppings.add("Shredded Mozzarella Cheese");
            toppings.add("Black Olives");
            toppings.add("Spinach");
            toppings.add("Eggplant");
            toppings.add("Sliced Pepperoni");
        }
    
        void cut() {
            System.out.println("Cutting the pizza into square slices");
        }
    }
    class ChicagoStyleVeggiePizza extends Pizza {
        public ChicagoStyleVeggiePizza() {
            name = "Chicago Deep Dish Veggie Pizza";
            dough = "Extra Thick Crust Dough";
            sauce = "Plum Tomato Sauce";
    
            toppings.add("Shredded Mozzarella Cheese");
            toppings.add("Black Olives");
            toppings.add("Spinach");
            toppings.add("Eggplant");
        }
    
        void cut() {
            System.out.println("Cutting the pizza into square slices");
        }
    }
    class NYPizzaStore extends PizzaStore {
    
        Pizza createPizza(String item) {
            if (item.equals("cheese")) {
                return new NYStyleCheesePizza();
            }
            else return null;
        }
    }
    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");
        }
    }

  • 相关阅读:
    关于application/x-www-form-urlencoded编码
    socket 状态
    TCP/IP三次握手详解
    一步步优化JVM四:决定Java堆的大小以及内存占用
    编码问题
    git 应用
    父与子继承
    python try except
    python 编码问题
    requests 库使用方法
  • 原文地址:https://www.cnblogs.com/cckong/p/13768339.html
Copyright © 2020-2023  润新知