Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new DecoyDuck(); } else if (inBathTub) { duck = new RubberDuck(); }
public Pizza orderPizza(String type) { Pizza pizza; //根据顾客的口味匹配不同种类的披萨 //问题出现了,随着时间的变化,披萨的菜单会改变,这里就必须已改再改 if (type.equals("cheese")) { pizza = new CheesePizza(); } else if { } //这个是我们不想改变的地方。因为披萨的准备、烘烤、切片、包装,多年来都保持不变, //所以这部分代码不会改变,只有发生这些动作的披萨会改变 pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }
public class SimplePizzaFactory { //SimplePizzaFactory只做一件事情,帮它的客户创建Pizza //首先,在这个工厂内定义一个createPizza()方法。所有客户用这个方法来实例化新对象 public Pizza createPizza(String type) { Pizza pizza = null; //这个代码没有什么变动,和原本orderPizza()方法中的代码一样,依然是以pizza的类型为参数 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; } }
public class PizzaStore { //为PizzaStore加上一个对SimplePizzaFactory的引用 SimplePizzaFactory factory; //PizzaStore的构造器,需要一个工厂作为参数 public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; //而orderPizza()方法通过简单传入订单类型来使用工厂创建pizza //请注意,我们把new操作符替换成工厂对象的创建方法,这里不再使用具体实例化! pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
public abstract class PizzaStore { /* * 工厂方法用来处理对象的创建,并将这样的行为封装在子类中。 * 这样,客户程序中关于超类的代码就和子类对象创建代码解耦了。 * 1、工厂方法是抽象的,所以依赖子类来处理对象的创建; * 2、工厂方法必须返回一个产品。超类中定义的方法,通常使用到工厂方法的返回值; * 3、工厂方法将客户(也就是超类中的代码,例如orderPizza())和实际创建具体产品的代码分离 */ 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; } }
public abstract class Pizza { //每个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 + " ----\n"); display.append(dough + "\n"); display.append(sauce + "\n"); for (int i = 0; i < toppings.size(); i++) { display.append((String )toppings.get(i) + "\n"); } return display.toString(); } }
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"); } }
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"); } //这个Pizza覆盖了cut()方法,将Pizza切成了正方形 void cut() { System.out.println("Cutting the pizza into square slices"); } }
public class NYPizzaStore extends PizzaStore { Pizza createPizza(String item) { if (item.equals("cheese")) { return new NYStyleCheesePizza(); } else if (item.equals("veggie")) { return new NYStyleVeggiePizza(); } else if (item.equals("clam")) { return new NYStyleClamPizza(); } else if (item.equals("pepperoni")) { return new NYStylePepperoniPizza(); } else return null; } }
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 interface PizzaIngredientFactory { //在接口中,每个原料都有一个对应的方法创建该原料 //如果每个工厂实例内都有某一种通用的“机制”需要实现,就可以把这个例子改写成抽象类 public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); public Veggies[] createVeggies(); public Pepperoni createPepperoni(); public Clams createClam(); }
public class NYPizzaIngredientFactory implements PizzaIngredientFactory { //对于原料家族内的每一种原料,我们都提供了纽约的版本 public Dough createDough() { return new ThinCrustDough(); } public Sauce createSauce() { return new MarinaraSauce(); } public Cheese createCheese() { return new ReggianoCheese(); } public Veggies[] createVeggies() { Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() }; return veggies; } public Pepperoni createPepperoni() { return new SlicedPepperoni(); } public Clams createClam() { return new FreshClams(); } }
public abstract class Pizza { String name; //每个Pizza都有一组在准备时会用到的原料 Dough dough; Sauce sauce; Veggies veggies[]; Cheese cheese; Pepperoni pepperoni; Clams clam; //现在把prepare()方法生命诚抽象。在这个方法中,我们需要收集披萨所需的原料,而这些原料当然是来自原料工厂了。 abstract void prepare(); 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"); } void setName(String name) { this.name = name; } String getName() { return name; } public String toString() { StringBuffer result = new StringBuffer(); result.append("---- " + name + " ----\n"); if (dough != null) { result.append(dough); result.append("\n"); } if (sauce != null) { result.append(sauce); result.append("\n"); } if (cheese != null) { result.append(cheese); result.append("\n"); } if (veggies != null) { for (int i = 0; i < veggies.length; i++) { result.append(veggies[i]); if (i < veggies.length-1) { result.append(", "); } } result.append("\n"); } if (clam != null) { result.append(clam); result.append("\n"); } if (pepperoni != null) { result.append(pepperoni); result.append("\n"); } return result.toString(); } }
public class CheesePizza extends Pizza { PizzaIngredientFactory ingredientFactory; //要之作Pizza,需要工厂提供原料。所以每个Pizza类都需要从构造器参数中得到一个工厂, //并把这个工厂存储在一个实例变量中 public CheesePizza(PizzaIngredientFactory ingredientFactory) { this.ingredientFactory = ingredientFactory; } //神奇的事情发生在这里,prepare()方法一步一步地创建芝士Pizza,每当需要原材料,就跟工厂要 void prepare() { System.out.println("Preparing " + name); dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); } }
public class NYPizzaStore extends PizzaStore { protected Pizza createPizza(String item) { Pizza pizza = null; //纽约店会用到纽约Pizza原料工厂,由该原料工厂负责生产所有原料 PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); /* * 把工厂传递给每一个Pizza,以便能从工厂中取得原料 * 对于每一种Pizza,我们实例化一个新的Pizza,并传进该中Pizza所需的工厂,以便Pizza取得它的原材料 */ if (item.equals("cheese")) { pizza = new CheesePizza(ingredientFactory); pizza.setName("New York Style Cheese Pizza"); } else if (item.equals("veggie")) { pizza = new VeggiePizza(ingredientFactory); pizza.setName("New York Style Veggie Pizza"); } else if (item.equals("clam")) { pizza = new ClamPizza(ingredientFactory); pizza.setName("New York Style Clam Pizza"); } else if (item.equals("pepperoni")) { pizza = new PepperoniPizza(ingredientFactory); pizza.setName("New York Style Pepperoni Pizza"); } return pizza; } }
原文:http://blog.csdn.net/adoaiwen1314/article/details/22729749