工厂模式
目的:为了减少应用程序与具体类之间的依赖(松耦合),将对象的创建进行封装。所有的工厂都是用来封装对象的创建。
- 简单工厂模式。(简单工程其实不是一个设计模式,反而像是一种编程习惯)
- 工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
- 抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体的类。
简单工厂
代码示例:
public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } /** * 订披萨 * @param type * @return */ Pizza orderPizza(String type) { Pizza pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } /** * 简单工厂模式。(简单工程其实不是一个设计模式,反而像是一种编程习惯) * 这是实例工厂,方法是实例方法。 * 若将方法加上static,就是静态工厂。 */ public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if("cheese".equals(type)) { pizza = new ChessePizza(); }else if("pepperoni".equals(type)) { pizza = new PepperoniPizza(); }else if("clam".equals(type)) { pizza = new ClamPizza(); }else if("veggie".equals(type)) { pizza = new VeggiePizza(); } return pizza; } } 奶酪 意大利辣香肠 蛤蜊 素食 ChessePizza、PepperoniPizza、ClamPizza、VeggiePizza 都是 Pizza 类的子类。
工厂方法模式
代码:
public abstract class PizzaStore { // 工厂方法 模式 abstract Pizza createPizza(String type); Pizza orderPizza(String type) { Pizza pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } public class NYPizzaStore extends PizzaStore { @Override Pizza createPizza(String type) { Pizza pizza = null; if("cheese".equals(type)) { pizza = new NYStyleCheesePizza(); }else if("pepperoni".equals(type)) { pizza = new NYStylePepperoniPizza(); }else if("clam".equals(type)) { pizza = new NYStyleClamPizza(); }else if("veggie".equals(type)) { pizza = new NYStyleVeggiePizza(); } return pizza; } } public class ChicagoPizzaStore extends PizzaStore { @Override Pizza createPizza(String type) { Pizza pizza = null; if("cheese".equals(type)) { pizza = new ChicagoStyleCheesePizza(); }else if("pepperoni".equals(type)) { pizza = new ChicagoStylePepperoniPizza(); }else if("clam".equals(type)) { pizza = new ChicagoStyleClamPizza(); }else if("veggie".equals(type)) { pizza = new ChicagoStyleVeggiePizza(); } return pizza; } }
抽象工厂模式
例子的UML:
代码:
public abstract class PizzaStore { abstract Pizza createPizza(String type); Pizza orderPizza(String type) { Pizza pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } public class NYPizzaStore extends PizzaStore { @Override Pizza createPizza(String type) { Pizza pizza = null; PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); if("cheese".equals(type)) { pizza = new CheesePizza(ingredientFactory); pizza.setName("New York Style Cheese Pizza"); }else if("pepperoni".equals(type)) { pizza = new PepperoniPizza(ingredientFactory); }else if("clam".equals(type)) { pizza = new ClamPizza(ingredientFactory); }else if("veggie".equals(type)) { pizza = new VeggiePizza(ingredientFactory); } return pizza; } } public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); //... } public class NYPizzaIngredientFactory implements PizzaIngredientFactory { @Override public Dough createDough() { return new ThinCrustDough(); } @Override public Sauce createSauce() { return new MarinaraSauce(); } @Override public Cheese createCheese() { return new ReggianoCheese(); } } public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { @Override public Dough createDough() { return new ThickCrustDough(); } @Override public Sauce createSauce() { return new PlumTomatoSauce(); } @Override public Cheese createCheese() { return new MozzarellaCheese(); } }
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。