工厂方法模式
来源:互联网 发布:asp工资管理系统源码 编辑:程序博客网 时间:2024/05/05 04:41
之前有一篇简单工厂模式的介绍:http://liyf155.iteye.com/blog/1180975,里面介绍了简单工厂模式。简单工厂模式的缺点是很明显的,对“开-闭”原则的支持不够,即扩展性不好,如果有新的产品加入到系统中,那么就要去修改工厂类,并且将必要的逻辑加入到工厂类中,工厂方法模式的引进,既保留了简单工厂模式的有点,又克服了它的缺点。
首先,在工厂方法模式中,核心的工厂类不再负责所有的产品的创建,而是将具体创建的工作交给子类去做。这个核心类则摇身一变成了一个抽象工厂角色,仅负责给出子类需要实现得接口,而不用关心具体的实现细节。
这种进一步抽象化得结果,使这种工厂方法模式可以用来允许系统在不修改具体工厂角色的情况下引进新的产品,这一特点无疑使得工厂方法模式具有超过简单工厂模式的优越性。
下面给出一个例子:、
- package factorymethod;
- // 水果类接口
- public interface FruitFactory
- {
- void grow();
- void havest();
- void plant();
- }
package factorymethod;// 水果类接口public interface FruitFactory{void grow();void havest();void plant();}
- package factorymethod;
- // 园丁类接口,依赖水果类接口
- public interface FruitGardener
- {
- FruitFactory generate();
- }
package factorymethod;// 园丁类接口,依赖水果类接口public interface FruitGardener{FruitFactory generate();}
- package factorymethod;
- public class AppleGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Apple();
- }
- }
package factorymethod;public class AppleGardener implements FruitGardener{public FruitFactory generate(){return new Apple();}}
- package factorymethod;
- public class GrapeGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Grape();
- }
- }
package factorymethod;public class GrapeGardener implements FruitGardener{public FruitFactory generate(){return new Grape();}}
- package factorymethod;
- public class OrangeGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Orange();
- }
- }
package factorymethod;public class OrangeGardener implements FruitGardener{public FruitFactory generate(){return new Orange();}}
- package factorymethod;
- public class Apple implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Apple growing......");
- }
- public void havest()
- {
- System.out.println("Apple havested......");
- }
- public void plant()
- {
- System.out.println("Apple planted......");
- }
- }
package factorymethod;public class Apple implements FruitFactory{public void grow(){System.out.println("Apple growing......");}public void havest(){System.out.println("Apple havested......");}public void plant(){System.out.println("Apple planted......");}}
- package factorymethod;
- public class Orange implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Orange growing......");
- }
- public void havest()
- {
- System.out.println("Orange havested......");
- }
- public void plant()
- {
- System.out.println("Orange planted......");
- }
- }
package factorymethod;public class Orange implements FruitFactory{public void grow(){System.out.println("Orange growing......");}public void havest(){System.out.println("Orange havested......");}public void plant(){System.out.println("Orange planted......");}}
- package factorymethod;
- public class Grape implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Grape growing......");
- }
- public void havest()
- {
- System.out.println("Grape havested......");
- }
- public void plant()
- {
- System.out.println("Grape planted......");
- }
- }
package factorymethod;public class Grape implements FruitFactory{public void grow(){System.out.println("Grape growing......");}public void havest(){System.out.println("Grape havested......");}public void plant(){System.out.println("Grape planted......");}}
- package factorymethod;
- public class Test
- {
- public static void main(String[] args)
- {
- FruitGardener appleGardener = new AppleGardener();
- FruitGardener orangeGardener = new OrangeGardener();
- FruitGardener grapeGardener = new GrapeGardener();
- FruitFactory fruit = appleGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- System.out.println("***************************");
- fruit = orangeGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- System.out.println("***************************");
- fruit = grapeGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- }
- }
package factorymethod;public class Test{public static void main(String[] args){FruitGardener appleGardener = new AppleGardener();FruitGardener orangeGardener = new OrangeGardener();FruitGardener grapeGardener = new GrapeGardener();FruitFactory fruit = appleGardener.generate();fruit.plant();fruit.grow();fruit.havest();System.out.println("***************************");fruit = orangeGardener.generate();fruit.plant();fruit.grow();fruit.havest();System.out.println("***************************");fruit = grapeGardener.generate();fruit.plant();fruit.grow();fruit.havest();}}
从代码中分析可以看出,FruitGardener这个抽象工厂类依赖FruitFactory这个抽象产品类,这两个类只是定义了需要被子类实现的方法而已,而子类具体要怎么实现可以不管。当系统需要添加一个新的产品,比如说Banana香蕉,那么只要一个BananaGardener实现FruitGardener接口,Banana实现FruitFactory接口,就实现了系统的扩展功能。
- 工厂模式 -- 工厂方法
- 工厂模式-工厂方法
- 工厂模式-工厂方法
- 工厂 > 工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式-工厂方法模式
- 工厂模式---工厂方法模式
- 【模式】工厂方法模式
- 工厂方法,抽象工厂模式
- C#工厂模式-工厂方法
- 工厂模式 和工厂方法
- 工厂模式-C-工厂方法
- 工厂模式以及工厂方法
- 简单工厂、工厂方法、抽象工厂模式
- 简单工厂、工厂方法、抽象工厂模式
- 工厂模式(简单工厂、工厂方法)
- 简单工厂|工厂方法|抽象工厂模式
- 设计模式-工厂模式(简单工厂模式+工厂方法模式)
- PHP实践之路(三)PHP初探数据类型
- [Paper Study] Polychronization: Computation with Spikes
- 装饰者模式
- 50幅惊艳的分形艺术作品
- Oracle自动生成编号的函数
- 工厂方法模式
- Servlet重构
- 苹果脚本-How to send a message using iChat and AppleScript
- [Code] Polychronization and resonance occur in 5-neuron-SNN
- 一个简易的WebServer程序
- 内存管理之虚拟内存篇
- Oracle 索引介绍
- 64bit服务器上运行32bit DLL文件报错的解决办法
- [二分答案][网络流验证][BZOJ 3232]圈地游戏