设计模式——创建型_工厂模式

  • 简单工厂(Simple Factory)

  • 工厂方法(Factory Method)

  • 抽象工厂(Abstract Factory)


1. 简单工厂

  • 产品相关
    • Product总接口
      • public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
    • 产品实现类:产品A和产品B
      • public class ProductA implements Product {
            public ProductA() {
                System.out.println("ProductA");
            }
        }
        
        public class ProductB implements Product {
            public ProductB() {
                System.out.println("ProductB");
            }
        }
  • 工厂相关
    • public class Factory {    //可以在工厂类中添加任何你所需要的逻辑
          public static Product create(String str){
              if(str.equalsIgnoreCase("ProductA")){    //生成ProductA
                  return new ProductA();
              }
              else if(str.equalsIgnoreCase("ProductB")){     //生成ProductB  
                  return new ProductB();
              }
              return null;
           }
      }
  • 客户端创建
    • public class Client {
          public static void main(String[] args) {    //调用Factory的静态方法生成所要的类   
              Factory.create("productA");
              Factory.create("ProductB");
          }
      }

简单工厂

  • 优点:简单
  • 缺点:违反了设计模式的 ocp原则(对扩展开放,对修改关闭)。即:当我们给类增加新功能的时候,尽量不修改代码,或者少修改代码。
    • eg. 如果需要增加ProductC产品,则需要新增if-else的判断

2. 工厂方法

  • 产品相关
    • Product总接口
      • public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
    • 产品A和产品B
      • public class ProductA implements Product {
            public ProductA() {
                System.out.println("ProductA");
            }
        }
        
        public class ProductB implements Product {
            public ProductB() {
                System.out.println("ProductB");
            }
        }
  • 工厂相关
    • Factory总接口
      • public interface Factory {    //声明产生产品类的方法
            public Product createProduct();
        }
    • Factory实现类:FactoryA 和 FactoryB
      • public class FactoryA implements Factory {    //实现工厂类的方法生成产品类A
            public Product createProduct(){
                return new ProductA();
            }
        }
        
        public class FactoryB implements Factory {    //实现工厂类的方法生成产品类B
            public Product createProduct(){
                return new ProductB();
            }
        }
  • 客户端创建
    • public class Client {
          public static void main(String[] args) {
              Factory factory;
              factory = new FactoryA();
              factory.createProduct();
              factory = new FactoryB();
              factory.createProduct();
          }
      }

工厂方法

  • 优点:实现了“开发-封闭”原则,无论增加多少产品类,不用修改原来的代码,只需增加Product和Factory类
  • 缺点:如果产品过多,需要增加的工厂类也会过多。同时只能针对一类产品,如果需要组合,则无法实现

3. 抽象工厂

抽象工厂解决了上面两种只能针对一类产品的问题,这里为不同产品附加对应的产地,也就是说不同Product的Location不同

  • 产品相关
    • Product总接口
      • public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
    • 产品实现类:产品A和产品B
      • public class ProductA implements Product {
            public ProductA() {
                System.out.println("ProductA");
            }
        }
        
        public class ProductB implements Product {
            public ProductB() {
                System.out.println("ProductB");
            }
        }
  • 产地相关
    • Location总接口
      • public interface Location{}//声明类所需继承的共同接口,也可以是抽象类
    • 产地实现类:产地X和产地Y
      • public class LocationX implements Location {
            public LocationX() {
                System.out.println("LocationX");
            }
        }
        
        public class LocationY implements Location {
            public LocationY() {
                System.out.println("LocationY");
            }
        }
  • 工厂相关
    • Factory总接口
      • public interface AbstractFactory{
            public Product createProduct();
            public Location createLocation(); 
        }
    • 对应Factory的实现类:FactoryA 和 FactoryB
      • public class FactoryA implements AbstractFactory {
            @Override
            public Product createProduct(){
                return new ProductA();
            }
            @Override
            public Gift createLocation(){
                return new LocationX();
            }
        }
        
        public class FactoryB implements AbstractFactory {
            @Override
            public Product createProduct(){
                return new ProductB();
            }
            @Override
            public Gift createLocation(){
                return new LocationY();
            }
        }
  • 客户端创建
    • public class Client {
          public static void main(String[] args) {
              AbstarctFactory factory;
              factory = new FactoryA();
              factory.createProduct();
              factory.createLocation();
              factory = new FactoryB();
              factory.createProduct();
              factory.createLocation();
          }
      }

类图:待补充

抽象工厂

  • 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合,在Factory中多加了一层
  • 优点:程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。
  • 缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值