工厂方法模式

工厂方法模式(Factory Method Pattern),是简单工厂模式的扩展,其英文原话是"Define an interface for creating an object,but let the subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses",翻译是:定义一个创建对象的借口,但是让子类来觉得该实例化那个类。工厂方法让一个类推迟实例化至它的子类中。工厂方法模式有四个角色:

抽象工厂(Creator)角色:这是工厂方法模式的核心,定义了创建产品的方法。与应用系统无关。具体工厂角色必须实现这个接口才能创建对象。

具体工厂(Concrete Creator)角色:实现了抽象工厂角色的接口,含有与应用密切相关的逻辑,并且接受应用程序的调用以创建产品对象。

抽象产品(Product)角色:定义了产品的共性,实现对产品最抽象的定义。

具体产品(Concrete Product)角色:实现抽象产品角色的接口。工厂方法所创建的对象就是该角色的实例。

工厂方法模其类图如下:

                                                                   

抽象工厂的代码模式:

[html] view plain copy
 
  1. public interface Creator{  
  2.     //这就是工厂方法,提供具体产品的类对象即可创建产品;  
  3.     public <T extends Product> T factory(Class<T> c);  
  4. }  

抽象产品的代码模式:

[html] view plain copy
 
  1. public interface Product{  
  2.     //产品的公共方法  
  3.     public void method1();  
  4.     public void method2();  
  5. }  

具体工厂的代码模式:

[html] view plain copy
 
  1. public class ConcreteCreator implements Creator{  
  2.   
  3.     //具体工厂生产的具体产品  
  4.     Product product = null;  
  5.     @Override  
  6.     public <T extends Product> T factory(Class<T> c) {  
  7.         // TODO Auto-generated method stub  
  8.         //生产出产品  
  9.         product = (Product) Class.forName(c.getName()).newInstance();  
  10.         //或这样  
  11.         //product = c.newInstance();  
  12.         return (T)product;  
  13.     }  
  14. }  

具体产品的代码模式:

[html] view plain copy
 
  1. public class ConcreteProduct implements Product{  
  2.   
  3.     @Override  
  4.     public void method1() {  
  5.         //do somthing  
  6.     }  
  7.   
  8.     @Override  
  9.     public void method2() {  
  10.         //do something  
  11.     }  
  12. }  

下面通过一个demo,来展示下工厂方法模式的应用;该例子是工厂生产水果的例子,首先需要一个抽象工厂Factory和抽象水果Fruit,Factory定义了生产水果的方法,Fruit定义了水果的共性;然后再由具体的工厂生产具体的水果,如AppleFactory(implements Factory)生产Apple(implements Fruit);GrapeFactory(implements Factory)生产Grape(implements Fruit),下面上代码。

Factory类

[html] view plain copy
 
  1. //抽象工厂,定义了生产产品的接口;  
  2. public interface Factory {  
  3.     /**  
  4.      * @param c 具体产品的Class对象;  
  5.      * @return 返回产品的父类,具体工厂调用得到产品时需要转型;  
  6.      */  
  7.     public Fruit factory(Class c);  
  8. }  

Fruit类:

[html] view plain copy
 
  1. //抽象产品,定义了产品的共性;  
  2. public interface Fruit {  
  3.     public void plant();  
  4.     public void grow();  
  5.     public void harvest();  
  6. }  

AppleFactory类:

[html] view plain copy
 
  1. //具体工厂,生产具体的产品,AppleFactory生产Apple  
  2. public class AppleFactory implements Factory {  
  3.       
  4.     @Override  
  5.     public Apple factory(Class c) {  
  6.         // TODO Auto-generated method stub  
  7.         try {  
  8.             //生产苹果  
  9.             Apple apple = (Apple)Class.forName(c.getName()).newInstance();  
  10.             //或这样 Apple apple = (Apple) c.newInstance();  
  11.             apple.setTreeAge(0);  
  12.             return apple;  
  13.         } catch (InstantiationException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         } catch (IllegalAccessException e) {  
  17.             // TODO Auto-generated catch block  
  18.             e.printStackTrace();  
  19.         }catch (ClassNotFoundException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         }  
  23.         return null;  
  24.     }  
  25. }  



GrapeFactory类:

[html] view plain copy
 
  1. public class GrapeFactory implements Factory {  
  2.   
  3.     @Override  
  4.     public Grape factory(Class c) {  
  5.         // TODO Auto-generated method stub  
  6.         try {  
  7.             Grape grape = (Grape)Class.forName(c.getName()).newInstance();  
  8.             //或这样 Grape grape = (Grape) c.newInstance();  
  9.             grape.setSeedless(true);  
  10.             grape.setTreeAge(0);  
  11.             return grape;  
  12.         } catch (InstantiationException e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.         } catch (IllegalAccessException e) {  
  16.             // TODO Auto-generated catch block  
  17.             e.printStackTrace();  
  18.         } catch (ClassNotFoundException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         }  
  22.         return null;  
  23.     }  
  24. }  



Apple类:

[html] view plain copy
 
  1. //具体产品  
  2. public class Apple implements Fruit {  
  3.   
  4.     private int treeAge;  
  5.       
  6.     @Override  
  7.     public void plant() {  
  8.         // TODO Auto-generated method stub  
  9.         System.out.println("Apple Tree is planted");  
  10.     }  
  11.   
  12.     @Override  
  13.     public void grow() {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("Apple Tree is growing");  
  16.     }  
  17.   
  18.     @Override  
  19.     public void harvest() {  
  20.         // TODO Auto-generated method stub  
  21.         System.out.println("Apple Tree is harvesting");  
  22.     }  
  23.   
  24.     /**  
  25.      * @return the treeAge  
  26.      */  
  27.     public int getTreeAge() {  
  28.         return treeAge;  
  29.     }  
  30.   
  31.     /**  
  32.      * @param treeAge the treeAge to set  
  33.      */  
  34.     public void setTreeAge(int treeAge) {  
  35.         this.treeAge = treeAge;  
  36.     }  
  37.   
  38.     /* (non-Javadoc)  
  39.      * @see java.lang.Object#toString()  
  40.      */  
  41.     @Override  
  42.     public String toString() {  
  43.         return "Apple [treeAge=" + treeAge + "]";  
  44.     }  
  45. }  



Grape类:

[html] view plain copy
 
  1. //具体产品  
  2. public class Grape implements Fruit {  
  3.       
  4.     boolean seedless;  
  5.     private int treeAge;  
  6.     @Override  
  7.     public void plant() {  
  8.         // TODO Auto-generated method stub  
  9.         System.out.println("Grape is palnted");  
  10.     }  
  11.   
  12.     @Override  
  13.     public void grow() {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("Grape is growing");  
  16.     }  
  17.   
  18.     @Override  
  19.     public void harvest() {  
  20.         // TODO Auto-generated method stub  
  21.         System.out.println("Grape is harvesting");  
  22.     }  
  23.   
  24.     /**  
  25.      * @return the seedless  
  26.      */  
  27.     public boolean isSeedless() {  
  28.         return seedless;  
  29.     }  
  30.   
  31.     /**  
  32.      * @return the treeAge  
  33.      */  
  34.     public int getTreeAge() {  
  35.         return treeAge;  
  36.     }  
  37.   
  38.     /**  
  39.      * @param seedless the seedless to set  
  40.      */  
  41.     public void setSeedless(boolean seedless) {  
  42.         this.seedless = seedless;  
  43.     }  
  44.   
  45.     /**  
  46.      * @param treeAge the treeAge to set  
  47.      */  
  48.     public void setTreeAge(int treeAge) {  
  49.         this.treeAge = treeAge;  
  50.     }  
  51.   
  52.     /* (non-Javadoc)  
  53.      * @see java.lang.Object#toString()  
  54.      */  
  55.     @Override  
  56.     public String toString() {  
  57.         return "Grape [seedless=" + seedless + ", treeAge=" + treeAge + "]";  
  58.     }  
  59.   
  60. }  



主方法入口类FactoryMethodTest:

[html] view plain copy
 
  1. public class FactoryMethodTest {  
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         //生产产品的工厂,以父类引用标示;或者也可以分别定义GrapeFactory和AppleFactory,这样fac.factory()这句就不用转型了;  
  9.         Factory fac ;  
  10.         //具体产品  
  11.         Apple apple;  
  12.         Grape grape;  
  13.         fac = new AppleFactory();  
  14.         //Apple的Class对象传给factory方法;  
  15.         apple = (Apple) fac.factory(Apple.class);  
  16.         System.out.println(apple);  
  17.         apple.plant();  
  18.         apple.grow();  
  19.         apple.harvest();  
  20.           
  21.         fac = new GrapeFactory();  
  22.         grape = (Grape)fac.factory(Grape.class);  
  23.         System.out.println(grape);  
  24.         grape.plant();  
  25.         grape.grow();  
  26.         grape.harvest();  
  27.     }  
  28. }  


运行结果:

Apple [treeAge=0]
Apple Tree is planted
Apple Tree is growing
Apple Tree is harvesting
Grape [seedless=true, treeAge=0]
Grape is palnted
Grape is growing
Grape is harvesting

总结:

工厂方法模式就是由具体的工厂生产具体的产品,当增加新产品时,只需增加一个新工厂即可。

转自:http://blog.csdn.net/liruikqn/article/details/12885491

原文地址:https://www.cnblogs.com/wwjldm/p/7215827.html