工厂方法模式

来源:互联网 发布:asp工资管理系统源码 编辑:程序博客网 时间:2024/05/05 04:41
之前有一篇简单工厂模式的介绍:http://liyf155.iteye.com/blog/1180975,里面介绍了简单工厂模式。简单工厂模式的缺点是很明显的,对“开-闭”原则的支持不够,即扩展性不好,如果有新的产品加入到系统中,那么就要去修改工厂类,并且将必要的逻辑加入到工厂类中,工厂方法模式的引进,既保留了简单工厂模式的有点,又克服了它的缺点。

       首先,在工厂方法模式中,核心的工厂类不再负责所有的产品的创建,而是将具体创建的工作交给子类去做。这个核心类则摇身一变成了一个抽象工厂角色,仅负责给出子类需要实现得接口,而不用关心具体的实现细节。

       这种进一步抽象化得结果,使这种工厂方法模式可以用来允许系统在不修改具体工厂角色的情况下引进新的产品,这一特点无疑使得工厂方法模式具有超过简单工厂模式的优越性。

       下面给出一个例子:、

Fruitfactory 代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. // 水果类接口   
  4. public interface FruitFactory   
  5. {   
  6.     void grow();   
  7.     void havest();   
  8.     void plant();   
  9. }  
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. // 园丁类接口,依赖水果类接口   
  4. public interface FruitGardener   
  5. {   
  6.     FruitFactory generate();   
  7. }  

 

Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class AppleGardener implements FruitGardener   
  4. {   
  5.     public FruitFactory generate()   
  6.     {   
  7.         return new Apple();   
  8.     }   
  9. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class GrapeGardener implements FruitGardener   
  4. {   
  5.     public FruitFactory generate()   
  6.     {   
  7.         return new Grape();   
  8.     }   
  9. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class OrangeGardener implements FruitGardener   
  4. {   
  5.     public FruitFactory generate()   
  6.     {   
  7.         return new Orange();   
  8.     }   
  9. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class Apple implements FruitFactory   
  4. {   
  5.     public void grow()   
  6.     {   
  7.         System.out.println("Apple growing......");   
  8.     }   
  9.   
  10.     public void havest()   
  11.     {   
  12.         System.out.println("Apple havested......");   
  13.     }   
  14.   
  15.     public void plant()   
  16.     {   
  17.         System.out.println("Apple planted......");   
  18.     }   
  19. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class Orange implements FruitFactory   
  4. {   
  5.     public void grow()   
  6.     {   
  7.         System.out.println("Orange growing......");   
  8.     }   
  9.   
  10.     public void havest()   
  11.     {   
  12.         System.out.println("Orange havested......");   
  13.     }   
  14.   
  15.     public void plant()   
  16.     {   
  17.         System.out.println("Orange planted......");   
  18.     }   
  19. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class Grape implements FruitFactory   
  4. {   
  5.     public void grow()   
  6.     {   
  7.         System.out.println("Grape growing......");   
  8.     }   
  9.   
  10.     public void havest()   
  11.     {   
  12.         System.out.println("Grape havested......");   
  13.     }   
  14.   
  15.     public void plant()   
  16.     {   
  17.         System.out.println("Grape planted......");   
  18.     }   
  19. }  
 
Java代码 复制代码 收藏代码
  1. package factorymethod;   
  2.   
  3. public class Test   
  4. {   
  5.     public static void main(String[] args)   
  6.     {   
  7.         FruitGardener appleGardener = new AppleGardener();   
  8.         FruitGardener orangeGardener = new OrangeGardener();   
  9.         FruitGardener grapeGardener = new GrapeGardener();   
  10.         FruitFactory fruit = appleGardener.generate();   
  11.         fruit.plant();   
  12.         fruit.grow();   
  13.         fruit.havest();   
  14.         System.out.println("***************************");   
  15.         fruit = orangeGardener.generate();   
  16.         fruit.plant();   
  17.         fruit.grow();   
  18.         fruit.havest();   
  19.         System.out.println("***************************");   
  20.         fruit = grapeGardener.generate();   
  21.         fruit.plant();   
  22.         fruit.grow();   
  23.         fruit.havest();   
  24.     }   
  25. }  

 

        从代码中分析可以看出,FruitGardener这个抽象工厂类依赖FruitFactory这个抽象产品类,这两个类只是定义了需要被子类实现的方法而已,而子类具体要怎么实现可以不管。当系统需要添加一个新的产品,比如说Banana香蕉,那么只要一个BananaGardener实现FruitGardener接口,Banana实现FruitFactory接口,就实现了系统的扩展功能。

原创粉丝点击