栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

学习记录之工厂模式——创建型模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习记录之工厂模式——创建型模式

介绍
  • 1. 引例
  • 2. 简单工厂模式
  • 3. 工厂方法模式
  • 4. 抽象工厂模式

工厂模式分为如下三种:

1. 引例

假设现有需求:实现一个用户在奶茶店订购奶茶的项目。用户输入奶茶的品种,奶茶店输出该奶茶的制作信息,完成订购。

  • 1)奶茶的品种很多(比如:PearlMilkTea、CoconutMilkTea等)
  • 2)奶茶的制作有prepare,produce,packaging

传统的实现方式:

  • 构建奶茶的一个抽象类,让各个种类的奶茶继承,并实现自己独有的方法。

  • 用户输入奶茶名,输出奶茶的制作信息
public class OrderTea {
    public  OrderTea(){
        Tea tea = null;
        String orderType;
        do {
            orderType = getType();
            if (orderType.equals("PearlMilkTea")){
                tea = new PearlMilkTea("PearlMilkTea");
            }else if (orderType.equals("CoconutMilkTea")){
                tea = new CoconutMilkTea("CoconutMilkTea");
            }else {
                break;
            }
            tea.prepare();
            tea.produce();
            tea.packaging();
        }while (true);
    }

    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入想要订购的茶:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
输入想要订购的茶:
PearlMilkTea
PearlMilkTea: 正在准备PearlMilkTea的原材料
PearlMilkTea: 正在制作中
PearlMilkTea: 制作完毕,正在打包
输入想要订购的茶:
CoconutMilkTea
CoconutMilkTea: 正在准备CoconutMilkTea的原材料
CoconutMilkTea: 正在制作中
CoconutMilkTea: 制作完毕,正在打包
  • 优点:容易理解,简单易操作。
  • 缺点:违反设计模式的OCP原则,即对扩展(提供方)开放,对修改(使用方)关闭。例如:当增加一个新的奶茶品种时,我们需要对下面的代码进行如下修改.
		// 使用方
		do {
            orderType = getType();
            if (orderType.equals("PearlMilkTea")){
                tea = new PearlMilkTea("PearlMilkTea");
            }else if (orderType.equals("CoconutMilkTea")){
                tea = new CoconutMilkTea("CoconutMilkTea");
            }
            // 新加的代码
			else if (orderType.equals("新增的品种")){
                tea = new CoconutMilkTea("新增的品种");
            } 
            
			else {
                break;
            }
            tea.prepare();
            tea.produce();
            tea.packaging();
        }while (true);

改进的思路:

  • 分析:如果在其他地方也存在创建Tea的代码,那么就意味着也需要进行修改,这样的情况在实际开发中往往有多处。
  • 思路:把创建Tea对象封装在一个类中,当增加新品种时只用修改该类即可,其他需要创建Tea对象的代码只用调用该类而不需要额外的修改。
2. 简单工厂模式

介绍:简单工厂模式定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)。当用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式。

public class SimpleFactory {
    public static Tea createTea(String orderType) {

        Tea tea = null;

        if (orderType.equals("PearlMilkTea")) {
            tea = new PearlMilkTea("PearlMilkTea");
        } else if (orderType.equals("CoconutMilkTea")) {
            tea = new CoconutMilkTea("CoconutMilkTea");
        }

        return tea;
    }
}

上面的SimpleFactory类将创建Tea对象实例的代码进行了封装,使用方只需要调用即可。当增加新的Tea品种时,不需要在每个使用方(OrderTea类)进行修改,只需要在SimpleFactory类中修改即可。符合OCP原则,对扩展开放,对修改关闭。

 public OrderTea2() {

        do {
            Tea tea = null;
            String orderType = getType();
            tea = SimpleFactory.createTea(orderType);

            if (tea != null){
                tea.prepare();
                tea.produce();
                tea.packaging();
            }else {
                System.out.println("订购失败,无相应品种!");
                break;
            }

        } while (true);
    }
3. 工厂方法模式

假设现增一个新的需求:客户在订奶茶时可以点不同口味的奶茶,例如:北京的PearlMilkTea、北京的CoconutMilkTea、伦敦的PearlMilkTea、伦敦的CoconutMilkTea。

思路1:使用简单工厂模式,创建不同的简单工厂类,比如BJTeaSimpleFactory、LDTeaSimpleFactory等等。但是考虑到项目的规模,软件的可维护性、可扩展性并不是很好。

思路2:使用工厂方法模式。

工厂方法模式设计方案: 将奶茶项目的实例化功能抽象成抽象方法,在不同的口味点餐子类中具体实现。

工厂方法模式: 定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将对象的实例化推迟到子类。

public abstract class OrderTea03 {

    abstract Tea createTea(String orderType);

    public OrderTea03() {
        Tea tea = null;
        do {
            String orderType = getType();
            // 由子类决定要实例化的类
            tea = createTea(orderType);

            if (tea != null) {
                tea.prepare();
                tea.produce();
                tea.packaging();
            } else {
                System.out.println("订购失败,无相应品种!");
                break;
            }
        } while (true);
    }

    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入想要订购的茶:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
public class BJOrderTea  extends OrderTea03{
    @Override
    Tea createTea(String orderType) {
        Tea tea = null;
        if (orderType.equals("PearlMilkTea")){
            tea = new BJPearlMilkTea("PearlMilkTea");
        }else if (orderType.equals("CoconutMilkTea")){
            tea = new BJCoconutMilkTea("CoconutMilkTea");
        }
        return tea;
    }
}
public class LDOrderTea extends OrderTea03{
    @Override
    Tea createTea(String orderType) {
        Tea tea = null;
        if (orderType.equals("PearlMilkTea")){
            tea = new LDPearlMilkTea("PearlMilkTea");
        }else if (orderType.equals("CoconutMilkTea")){
            tea = new LDCoconutMilkTea("CoconutMilkTea");
        }
        return tea;
    }
}
public class TeaStore {
    public static void main(String[] args) {
        String loc = "bj";
        if (loc.equals("bj")) {
            new BJOrderTea();
        } else {
            new LDOrderTea();
        }
    }
}
4. 抽象工厂模式

抽象工厂模式:
1)定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类。是简单工厂模式和工厂方法模式的整合。
2)从设计层面看,抽象工厂模式就是对简单工厂模式的进一步抽象。
3)将工厂抽象成两层,AbsFactory(抽象工厂)和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。

// 一个抽象工厂模式的抽象层
public interface AbsFactory {
    // 让下面的工厂子类具体实现
    public Tea create(String orderType);
}
// 工厂子类
public class BJFactory implements AbsFactory{
    @Override
    public Tea create(String orderType) {
        
        Tea tea = null;
        
        if (orderType.equals("PearlMilkTea")){
            tea = new BJPearlMilkTea("PearlMilkTea");
        }else if (orderType.equals("CoconutMilkTea")){
            tea = new BJCoconutMilkTea("CoconutMilkTea");
        }
        
        return tea;
    }
}
public class LDFactory implements AbsFactory {

    @Override
    public Tea create(String orderType) {
        
        Tea tea = null;
        
        if (orderType.equals("PearlMilkTea")) {
            tea = new LDPearlMilkTea("PearlMilkTea");
        } else if (orderType.equals("CoconutMilkTea")) {
            tea = new LDCoconutMilkTea("CoconutMilkTea");
        }
        
        return tea;
    }
}
// 使用方
public class OrderTea04 {

    AbsFactory factory;

    public OrderTea04(AbsFactory factory){
        setFactory(factory);
    }

    private void setFactory(AbsFactory factory) {
        Tea tea = null;
        String orderType = "";
        this.factory = factory;
        do {
            orderType = getType();
            tea = factory.createTea(orderType);
            if (tea != null){
                tea.prepare();
                tea.produce();
                tea.packaging();
            }else {
                System.out.println("订购失败!");
                break;
            }
        }while (true);
    }

    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入想要订购的茶:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
public class TeaStore {
    public static void main(String[] args) {
        new OrderTea04(new BJFactory());
    }
}

总结:

  • JDK中的Calender类中,用到了简单工厂模式。
  • 工厂模式的意义:将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦,从而提高项目的扩展和维护性。
  • 经验:1)创建对象实例时,不要直接new 类,而是把这个new类的动作放在一个工厂方法中并返回。2)不要让类继承具体类,而是继承抽象类或者是实现接口。3)不要覆盖基类中已经实现的方法。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301219.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号