设计模式--工厂模式

抽象工厂模式:


        1.简单工厂模式在实际中并不实用且缺点较明显。

        2.工厂方法模式解决了简单工厂不能实现“开闭原则”的弊端,但是代码会比较冗余。

       3.抽象工厂模式:Provide an interface for creating families of related or dependent objects without specifying their concrete classes。

抽象的产品:手机和充电器

//产品类(手机)
public interface Mobile {  
     String getName();  
}  
//产品类(充电器)
public interface Charger {  
    String getName();  
} 
public class AppleMobile implements Mobile {  
     private String name;  
  
     public AppleMobile() {  
          name = "苹果手机";  
          System.out.println("Apple手机生产完毕!");        
     }  
  
     @Override  
     public String getName() {  
          return name != null ? name : null;  
     }  
}  
public class HuaweiMobile implements Mobile {  
     private String name;  
  
     public HuaweiMobile() {  
          name = "华为手机";  
          System.out.println("华为手机生产完毕!");  
     }  
  
     @Override  
     public String getName() {  
          return name != null ? name : null;  
     }  
  
}  

手机和充电器:两个具体的品牌

public class AppleCharger implements Charger {  
     private String name;  
  
     public AppleCharger() {  
          name = "苹果手机充电器";  
          System.out.println("Apple手机充电器生产完毕!");  
     }  
  
     @Override  
     public String getName() {  
          return name != null ? name : null;  
     }  
}  

//+++++++++++++++++++++++++++++++
public class HuaweiCharger implements Charger {  
     private String name;  
  
     public HuaweiCharger() {  
          name = "华为充电器";  
          System.out.println("华为手机充电器生产完毕!");  
     }  
  
     @Override  
     public String getName() {  
          return name != null ? name : null;   
     }  
}  

已经有了产品样本,现在需要工厂去生产。

需要对工厂进行抽象:

public interface MobileFactory {  
     // 生产手机  
     Mobile getMobile();  
  
     // 生产充电器  
     Charger getCharger();  
}  

创建具体的工厂

public class AppleFactory implements MobileFactory {  
     // 生产苹果手机  
     @Override  
     public Mobile getMobile() {  
          return new AppleMobile();  
     }  
  
     // 生产苹果手机充电器  
     @Override  
     public Charger getCharger() {  
          return new AppleCharger();  
     }  
}  
public class HuaweiFactory implements MobileFactory {  
     // 生产华为手机  
     @Override  
     public Mobile getMobile() {  
          return new HuaweiMobile();  
     }  
  
     // 生产华为手机充电器  
     @Override  
     public Charger getCharger() {  
          return new HuaweiCharger();  
     }  
}  

测试使用工厂类:

public class Test {  
  
     public static void main(String[] args) {  
          AppleFactory appleFactory = new AppleFactory();  
          AppleMobile appleMobile = (AppleMobile) appleFactory.getMobile();  
          AppleCharger appleCharger = (AppleCharger) appleFactory.getCharger();  
  
          HuaweiFactory huaweiFactory = new HuaweiFactory();  
          HuaweiMobile huaweiMobile = (HuaweiMobile) huaweiFactory.getMobile();  
          HuaweiCharger huaweiCharger = (HuaweiCharger) huaweiFactory.getCharger();  
     }  
}  

总结:

    1.对产品进行抽象

    2.对工厂进行抽象

    3.具体的产品实现抽象的产品接口

    4.具体的工厂实现抽象工厂

    5.按需创建想要的class





        

猜你喜欢

转载自blog.csdn.net/sjp_359056/article/details/80009507