抽象产品和具体产品的代码和工厂方法模式一致,这里就不加赘述了,来看看抽象工厂的使用吧!
4、测试 public class PizzaStore { private AbstractFactory abstractFactory; public PizzaStore(AbstractFactory abstractFactory) { this.abstractFactory = abstractFactory; } public void prepare() { this.abstractFactory.createCheese(); this.abstractFactory.createDurian(); } } public class Test { public static void main(String[] args) { AbstractFactory abstractFactory = new AFactory(); PizzaStore pizzaStore = new PizzaStore(abstractFactory); pizzaStore.prepare(); AbstractFactory abstractFactory2 = new BFactory(); PizzaStore pizzaStore2 = new PizzaStore(abstractFactory2); pizzaStore2.prepare(); } }工厂方法和抽象工厂模式. (3)
内容版权声明:除非注明,否则皆为本站原创文章。