工厂模式-静态工厂

1.产品类

1.1产品的统一接口

[java] view plain copy
  1. /** 
  2.  * @Desc:产品的接口 
  3.  * @author zy 
  4.  * @date 2016年6月21日下午4:40:33 
  5.  */  
  6. public interface Product {  
  7.       
  8.     /** 
  9.      * @Desc:获取具体的产品 
  10.      * @author:zy 
  11.      * @version: 2016年6月21日 下午5:00:37 
  12.      */  
  13.     public void getDetailProduct();  
  14.       
  15. }  
1.2 A产品和B产品

[java] view plain copy
  1. public class A implements Product{  
  2.   
  3.     @Override  
  4.     public void getDetailProduct() {  
  5.         System.out.println("get a product");  
  6.           
  7.     }  
  8.   
  9. }  
  10.   
  11.   
  12.   
  13. public class B implements Product{  
  14.   
  15.     @Override  
  16.     public void getDetailProduct() {  
  17.         System.out.println("get b product");  
  18.           
  19.     }  
  20.       
  21. }  

2.静态工厂方法

/**
 * Description: 简单工厂的静态工厂(和简单工厂方法不同,是优化简单工厂方法,省去了传入参数,根据参数判断,参数可能会传入错误)
 * @author zy
 * @date 2016年6月21日下午4:18:38
 */
public class SimpleFactoryStatic {
	/**
	 * @Desc:获取a实例
	 * @return:
	 * @author:zy
	 * @version: 2016年6月21日 下午5:25:50
	 */
    public static Product getA(){
        return new A();
    }
    
    /**
     * @Desc:获取b实例
     * @return:
     * @author:zy
     * @version: 2016年6月21日 下午5:26:00
     */
    public static Product getB(){
        return new B();
    }
}

3.测试





tips:

  静态工厂,是工厂方法的改进。弥补了工厂方法模式,传入参数可能有误的问题,优化了工厂的生产调用

posted @ 2016-06-27 17:19  Bug开发攻城狮  阅读(128)  评论(0编辑  收藏  举报