设计模式(三)--工厂模式(工厂方法模式+抽象工厂模式)
程序员文章站
2022-05-17 15:03:12
...
工厂模式:具体的可以分为两种,分别是工厂方法模式 、抽象工厂模式。
工厂方法模式:定义了一个创建对象的接口,但是由子类决定要实例化的类是哪一个,工厂方法让类把实例化推迟到了子类。
设计原则:依赖倒置原则(要依赖抽象,而不是依赖具体类)。
遵守依赖倒置原则的几个建议:
1.变量不可以持有具体类的引用(如果使用new,就会持有具体类的引用,可以改用工厂来避开这样的做法)
2.不要让类派生自具体类(如果派生自具体类,你就会依赖具体类,请派生自一个抽象【接口或者抽象类】)
3.不要覆盖基类中已实现的方法(如果覆盖基类已实现的方法,那么你的基类就不是一个真正适合被继承的抽象。基类中已实现的方法,应该由所有的子类共享)
比较好的解释: http://blog.csdn.net/wyxhd2008/article/details/5597975
具体的工厂方法模式代码:
/** * Created by Administrator on 2017/12/9. * 产品类 */ public abstract class Pizza { protected String name; protected String dough; protected void prepare(){ System.out.println("prepare"); } protected void cut(){ System.out.println("cut"); } protected void box(){ System.out.println("box"); } }
/** * Created by Administrator on 2017/12/9. * 创建类 */ public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza = createPizza(type); //让子类自己去实现 pizza.prepare(); pizza.cut(); pizza.box(); return pizza; } /** * 让子类去根据情况实现创建Pizza * */ protected abstract Pizza createPizza(String type); }
/** * Created by Administrator on 2017/12/9. * 具体的实现的pizza */ public class CheesetPizza extends Pizza { public CheesetPizza() { this.name = "chesse"; this.dough = "doughchesse"; } @Override public String toString() { return "CheesetPizza " + this.name + ", " + this.dough; } }
/** * Created by Administrator on 2017/12/9. * 具体的pizzaStore */ public class NYStylePizzaStore extends PizzaStore { @Override protected Pizza createPizza(String type) { Pizza pizza = null; if("Cheese".equals(type)){ //根据不同类型创造不同的pizza pizza = new CheesetPizza(); }/*else if("clam".equals(type)){ //其他类型的Pizza }*/ return pizza; } }
/** * Created by Administrator on 2017/12/9. */ public class TestFactoryMethod { public static void main(String[] args) { PizzaStore pizzaStore = new NYStylePizzaStore(); Pizza pizza = pizzaStore.orderPizza("Cheese"); System.out.println(pizza); } }
抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
/** * Created by Administrator on 2017/12/9. */ public abstract class Pizza { protected Dough dough; protected abstract void prepare(); protected void cut(){ System.out.println("cut"); } protected void box(){ System.out.println("box"); } }
public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza = createPizza(type); //让子类自己去实现 pizza.prepare(); pizza.cut(); pizza.box(); return pizza; } /** * 让子类去根据情况实现创建Pizza * */ protected abstract Pizza createPizza(String type); }
/** * Created by Administrator on 2017/12/9. * pizza原料工厂 */ public interface PizzaIngredientFactory { Dough createDough(); }
/** * Created by Administrator on 2017/12/9. * pizza的原料[可以变成接口,产生多个品种的Dough] */ public class Dough { private String dough; public String getDough() { return dough; } public void setDough(String dough) { this.dough = dough; } }
/** * Created by Administrator on 2017/12/9. */ public class CheesePizaa extends Pizza{ private PizzaIngredientFactory pizzaIngredientFactory ; public CheesePizaa(PizzaIngredientFactory pizzaIngredientFactory) { this.pizzaIngredientFactory = pizzaIngredientFactory; } @Override protected void prepare() { this.dough = pizzaIngredientFactory.createDough();//通过工厂来产生原料 } }
/** * Created by Administrator on 2017/12/9. */ public class NYPizzaStore extends PizzaStore { @Override protected Pizza createPizza(String type) { Pizza pizza = null; PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory(); if("Cheese".equals(type)){ pizza = new CheesePizaa(pizzaIngredientFactory); }/*else if("clam".equals(type)){ }*/ return pizza; } }
/** * Created by Administrator on 2017/12/9. */ public class NYPizzaIngredientFactory implements PizzaIngredientFactory{ @Override public Dough createDough() { return new Dough(); } }
上一篇: 怎样使MySQL安全以对_PHP教程
下一篇: phpize的深入理解