• 【Java 设计模式】简单工厂模式 & 静态工厂模式


    §1 简单工厂模式

    • 简单工厂模式并不属于 23 种设计模式。

    §1.1 角色

    • 抽象产品:定义产品的规范,描述产品的主要特性和功能,如:咖啡。
    • 具体产品:实现或继承抽象产品的子类,如:美式咖啡,拿铁咖啡。
    • 具体工厂:提供创建产品的方式,供产品使用者调用获取具体产品,如:咖啡工厂生产咖啡,咖啡店通过咖啡工厂获取咖啡。

    §1.2 点咖啡案例

    • 简单工厂模式其实就是将产品的创建工作交由工厂进行,而不是让产品使用者自己去创建。
    • 以点咖啡为例,咖啡店(产品使用者)不用直接去 new 咖啡(具体产品),而是通过调用咖啡工厂(具体工厂)提供的方法来获取咖啡。

    §1.2.1 类图

    在这里插入图片描述

    §1.2.2 实现

    // 抽象产品: 咖啡类
    public abstract class Coffee {
        public abstract String getName();
        public void addMilk() {
            System.out.println("咖啡加奶");
        }
        public void addSugar() {
            System.out.println("咖啡加糖");
        }
    }
    
    // 具体产品: 美式咖啡类
    public class AmericanCoffee extends Coffee{
        @Override
        public String getName() {
            return "美式咖啡";
        }
    }
    
    // 具体产品: 拿铁咖啡类
    public class LatteCoffee extends Coffee{
        @Override
        public String getName() {
            return "拿铁咖啡";
        }
    }
    
    // 具体工厂: 咖啡工厂类
    public class SimpleCoffeeFactory {
        public Coffee createCoffee(String type) {
            Coffee coffee = null;
            if ("AmericanCoffee".equals(type)) {
                coffee = new AmericanCoffee();
            } else if ("LatteCoffee".equals(type)) {
                coffee = new LatteCoffee();
            } else {
                throw new RuntimeException("该产品暂未提供");
            }
            return coffee;
        }
    }
    
    // 咖啡店类
    public class CoffeeStore {
        public Coffee orderCoffee(String type) {
            SimpleCoffeeFactory factory = new SimpleCoffeeFactory();
            Coffee coffee = factory.createCoffee(type);
            coffee.addMilk();
            coffee.addSugar();
            return coffee;
        }
    }
    
    // 测试类
    public class Test {
        public static void main(String[] args) {
            // 1.创建一个咖啡类
            CoffeeStore coffeeStore = new CoffeeStore();
            // 2.点咖啡
            Coffee coffee = coffeeStore.orderCoffee("AmericanCoffee");
            System.out.println(coffee.getName());
        }
    }
    /* 测试结果:
    咖啡加奶
    咖啡加糖
    美式咖啡
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    §1.3 优点

    • 与对象解耦。封装了创建对象的过程,将对象的创建与业务逻辑分离开,避免修改客户的代码。如果出现了新的咖啡品种,直接修改咖啡工厂类即可,省去对咖啡店代码的修改。
    • 此时会有人疑问,既然都需要修改代码,为啥不直接修改咖啡店(产品使用者,客户)的代码呢?这是因为可能有很多咖啡店,难道每一个都要去修改一遍吗?这样是不是太麻烦了呢!

    §1.4 缺点

    • 虽然咖啡店(客户)代码无需修改,但是需要修改咖啡工厂(具体工厂)的代码,这样同样违反了设计原则中的开闭原则

    §2 静态工厂模式

    • 静态工厂模式也不属于 23 种设计模式。
    • 静态工厂模式可以说是简单工厂模式的扩展,只是将咖啡工厂(具体工厂)中创建咖啡(具体产品)的方法定义为静态方法而已。

    §2.1 代码变动

    // 具体工厂: 咖啡工厂类
    public class SimpleCoffeeFactory {
        public static Coffee createCoffee(String type) {
            Coffee coffee = null;
            if ("AmericanCoffee".equals(type)) {
                coffee = new AmericanCoffee();
            } else if ("LatteCoffee".equals(type)) {
                coffee = new LatteCoffee();
            } else {
                throw new RuntimeException("该产品暂未提供");
            }
            return coffee;
        }
    }
    
    
    // 咖啡店类
    public class CoffeeStore {
        public Coffee orderCoffee(String type) {
            Coffee coffee = SimpleCoffeeFactory.createCoffee(type);
            coffee.addMilk();
            coffee.addSugar();
            return coffee;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    §2.2 优点

    • 咖啡店(客户)在获取咖啡(具体产品)时,不需要创建咖啡工厂(具体工厂)对象,可以直接通过类名.方法名的方式来获取,避免创建多个工厂对象导致的资源浪费。
  • 相关阅读:
    1.1.3 基于centos的lamp
    科技云报道:发布分布式云战略,中国电子云吹响冲锋号角
    Docker:容器
    VoLTE端到端业务详解 | 基本概念
    关于如何找环形链表的入环点
    PASCAL VOC2012 数据集讲解与制作自己的数据集
    Miniconda 安装和使用笔记
    利用idea新创建maven项目时的一些基本配置
    信号发生器的电路构成及工作原理
    socket实现简单聊天【linux】
  • 原文地址:https://blog.csdn.net/Coder_Farmer/article/details/128009918