就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
举例:北京工厂和上海工厂
源文件:
FactoryInterface.java
public interface FactoryInterface { /** * 工厂的生产方法 */ void produce(); }
BejinFactory.java
public class BejinFactory implements FactoryInterface { @Override public void produce() { System.out.println("北京工厂开始生产"); } }
ShangHaiFactory.java
public class ShangHaiFactory implements FactoryInterface { @Override public void produce() { System.out.println("上海工厂开始生产"); } }
Factory.java
public class Factory { public FactoryInterface produce(String address){ if("北京".equals(address)){ return new BejinFactory(); }else if("上海".equals(address)){ return new ShangHaiFactory(); } return null; } }
测试
Test.java
public class Test { public static void main(String[] args) { Factory factory = new Factory(); FactoryInterface beijing = factory.produce("北京"); FactoryInterface shanghai = factory.produce("上海"); beijing.produce(); shanghai.produce(); } }
输入:
北京工厂开始生产
上海工厂开始生产
是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。
修改Factory.java
public class Factory { public FactoryInterface produceBeiJing() { return new BejinFactory(); } public FactoryInterface produceShangHai() { return new ShangHaiFactory(); } }
Test.java
public class Test { public static void main(String[] args) { Factory factory = new Factory(); FactoryInterface beijing = factory.produceBeiJing(); FactoryInterface shanghai = factory.produceShangHai(); beijing.produce(); shanghai.produce(); } }
输出:
北京工厂开始生产
上海工厂开始生产
将上面Factory中的方法设置为静态,则不用再创建Factory的实例
Factory.java
public class Factory { public static FactoryInterface produceBeiJing() { return new BejinFactory(); } public static FactoryInterface produceShangHai() { return new ShangHaiFactory(); } }
Test.java
public class Test { public static void main(String[] args) { FactoryInterface beijing = Factory.produceBeiJing(); FactoryInterface shanghai = Factory.produceShangHai(); beijing.produce(); shanghai.produce(); } }
输出:
北京工厂开始生产
上海工厂开始生产
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。
代码:
Factory.java
public interface Factory { FactoryInterface produce(); }
BeiJingFactory.java
public class BeiJingFactory implements Factory { @Override public FactoryInterface produce() { return new BeJingProducer(); } }
ShangHaiFactory.java
public class ShangHaiFactory implements Factory { @Override public FactoryInterface produce() { return new ShangHaiProducer(); } }
ShangHaiProducer.java
public class ShangHaiProducer implements FactoryInterface { @Override public void produce() { System.out.println("上海工厂开始生产"); } }
BeJingProducer.java
public class BeJingProducer implements FactoryInterface { @Override public void produce() { System.out.println("北京工厂开始生产"); } }
FactoryInterface.java
public interface FactoryInterface { /** * 工厂的生产方法 */ void produce(); }
Test.java
public class Test { public static void main(String[] args) { Factory beijingFactory = new ShangHaiFactory(); Factory shanghaiFactory = new BeiJingFactory(); FactoryInterface beijing = beijingFactory.produce(); FactoryInterface shanghai = shanghaiFactory.produce(); beijing.produce(); shanghai.produce(); } }
这个模式的好处就是,如果你现在想增加一个功能:增加广州工厂。则只需做一个实现类,实现FactoryInterface接口,同时做一个工厂类,实现Factory接口,就可以了,不需要改动其它的工厂类
Word 版下载地址:http://download.csdn.net/detail/yangwei19680827/7306727
原文:http://blog.csdn.net/wiker_yong/article/details/25165263