Head First设计模式-抽象工厂模式
一、整体代码
PizzaStore.java
public class PizzaTestDrive { public static void main(String[] args) {PizzaFactory factory = new NYPizzaFactory(); PizzaStore store = new PizzaStore(factory); Pizza pizza = store.orderPizza("cheese"); System.out.println("We ordered a " + pizza.getName() + "\n"); pizza = store.orderPizza("clam"); System.out.println("We ordered a " + pizza.getName() + "\n"); PizzaFactory factory1 = new ChicagoPizzaFactory(); PizzaStore store1 = new PizzaStore(factory); Pizza pizza1 = store1.orderPizza("cheese"); System.out.println("We ordered a " + pizza.getName() + "\n"); pizza1 = store1.orderPizza("clam"); System.out.println("We ordered a " + pizza.getName() + "\n"); }}
抽象工厂模式:提供了一个接口,用来创建相关和依赖对象的家族,而不需要明确指名具体类。