设计模式之工厂
简单工厂:
简单工厂利用静态方法返回一个类具体对象,因此也叫静态工厂模式。
工厂:
工厂方法一般造一种产品。
抽象工厂:
抽象工厂造许多个系列产品,系列之内的产品互相之间有依赖。
工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式的问题。首先完全实现“开闭原则”,实现了可扩展。其次更复杂的层次结构,可以应用于产品结果复杂的场合。工厂方法模式的对简单工厂模式进行了抽象。有一个抽象的Factory类(可以是抽象类和接口),这个类将不在负责具体的产品生产,而是只制定一些规范,具体的生产工作由其子类去完成。在这个模式中,工厂类和产品类往往可以依次对应。即一个抽象工厂对应一个抽象产品,一个具体工厂对应一个具体产品,这个具体的工厂就负责生产对应的产品。工厂方法模式(Factory Method pattern)是最典型的模板方法模式(Templete Method pattern)应用。
工厂方法模式和简单工厂模式在结构上的不同是很明显的。工厂方法模式的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。工厂方法模式可以允许很多具体工厂类从抽象工厂类中将创建行为继承下来,从而可以成为多个简单工厂模式的综合,进而推广了简单工厂模式。工厂方法模式退化后可以变得很像简单工厂模式。设想如果非常确定一个系统只需要一个具体工厂类,那么就不妨把抽象工厂类合并到具体的工厂类中去。由于反正只有一个具体工厂类,所以不妨将工厂方法改成为静态方法,这时候就得到了简单工厂模式。
抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,创建多个产品族中的产品对象。这就是抽象工厂模式的用意。 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。 Abstract Factory是从工厂方法演化过来的,抽象工厂模式与工厂方法模式的最大区别就在于,工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则需要面对多个产品等级结构。一般的工厂模式只能生产一种产品,但是抽象工厂所提供的接口可以创建产品家族,利用这个接口书写代码,我们代码就从实际工厂解耦了,以便在不同上下文环境中实现各种各样的工厂制造各种各样的产品。
当你发现,有一个接口可以有多种实现的时候,可以考虑使用工厂方法来创建实例。当你返现,有一组接口可以有多种实现方案的时候,可以考虑使用抽象工厂创建实例组。简单工厂几乎不太有使用得到的场合,因为功能过于单一,如果仅仅是返一个确定的对象,那么事实上直接采用new对象出来,并无太多区别。当然如果序列类分离模块时,还是有一点点的使用价值。
简单工厂:
public class SimpleFactory { interface Car{ public abstract void speed(); } class BenZCar implements Car{ public void speed() { System.out.println("speed is 360km/h."); } } class BWMCar implements Car{ public void speed(){ System.out.println("speed is 380km/h."); } } class CarFactory{ public Car product(String type){ if("benz".equals(type)){ return new BenZCar(); }else{ return new BWMCar(); } } } public static void main(String[] args){ Car car = new SimpleFactory().new CarFactory().product("benz"); car.speed(); } }
工厂:
public class Factory { interface Car{ public abstract void speed(); } class BenZCar implements Car{ public void speed() { System.out.println("speed is 360km/h."); } } class BWMCar implements Car{ public void speed(){ System.out.println("speed is 380km/h."); } } interface CarFactory{ public abstract Car product(); } class BenZCarFactory implements CarFactory{ public Car product() { return new BenZCar(); } } class BWMCarFactory implements CarFactory{ public Car product() { return new BWMCar(); } } public static void main(String[] args) { BenZCarFactory beanZCarFactory = new Factory().new BenZCarFactory(); Car benZCar = beanZCarFactory.product(); benZCar.speed(); } }
抽象工厂:
public class AbstractFactory { interface BigCar{ public abstract void speed(); //other... } interface SmallCar{ public abstract void speed(); //other.... } class BenZBigCar implements BigCar{ public void speed() { System.out.println("BenZBigCar's speed is ...."); } } class BenZSmallCar implements SmallCar{ public void speed() { System.out.println("BenZSmallCar's speed is ...."); } } class BWMBigCar implements BigCar{ public void speed() { System.out.println("BWMBigCar's speed is ...."); } } class BWMSmallCar implements SmallCar{ public void speed() { System.out.println("BWMSmallCar's speed is ...."); } } interface CarFactory{ public abstract BigCar productBigCar(); public abstract SmallCar productSmallCar(); } class BenZCarFactory implements CarFactory{ public BigCar productBigCar() { return new BenZBigCar(); } public SmallCar productSmallCar() { return new BenZSmallCar(); } } class BWMCarFactory implements CarFactory{ public BigCar productBigCar() { return new BWMBigCar(); } public SmallCar productSmallCar() { return new BWMSmallCar(); } } public static void main(String[] args){ BenZCarFactory benZCarFactory = new AbstractFactory().new BenZCarFactory(); BigCar bigCar = benZCarFactory.productBigCar(); bigCar.speed(); } }
上一篇: MySQL优化和运维之道
下一篇: flutter 配置问题