设计模式--工厂模式

抽象工厂模式:


        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





        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值