Java设计模式 简单工厂模式 工厂方法模式 抽象工厂模式 模版工厂模式 模式对比

发布于:2025-04-02 ⋅ 阅读:(18) ⋅ 点赞:(0)

目录

一、工厂模式(简单工厂模式)

二、工厂方法模式(Factory Method)

三、抽象工厂模式(Abstract Factory)

四、模板工厂模式(特殊实现)

五、模式对比


一、工厂模式(简单工厂模式)

核心思想:通过工厂类集中控制对象创建逻辑,隐藏对象实例化细节
实现方式

  1. 定义产品接口/抽象类
  2. 创建具体产品类
  3. 编写工厂类,通过静态方法根据输入参数创建对象
// 产品接口
interface Shape {
    void draw();
}

// 具体产品
class Circle implements Shape {
    public void draw() { System.out.println("画圆形"); }
}

class Square implements Shape {
    public void draw() { System.out.println("画方形"); }
}

// 简单工厂
class ShapeFactory {
    public static Shape createShape(String type) {
        return switch (type.toUpperCase()) {
            case "CIRCLE" -> new Circle();
            case "SQUARE" -> new Square();
            default -> throw new IllegalArgumentException();
        };
    }
}

// 使用
Shape shape = ShapeFactory.createShape("CIRCLE");

特点

  • ✅ 客户端与具体类解耦
  • ❌ 违反开闭原则(新增类型需修改工厂)

二、工厂方法模式(Factory Method)

核心思想:定义创建对象的抽象方法,由子类决定实例化的类
实现方式

  1. 定义抽象工厂类
  2. 每个产品对应具体工厂子类
// 抽象工厂
abstract class Dialog {
    abstract Button createButton();
    
    void render() {
        Button button = createButton();
        button.render();
    }
}

// 具体工厂
class WindowsDialog extends Dialog {
    Button createButton() { return new WindowsButton(); }
}

class WebDialog extends Dialog {
    Button createButton() { return new HTMLButton(); }
}

// 产品体系
interface Button {
    void render();
}

class WindowsButton implements Button {
    public void render() { System.out.println("Windows风格按钮"); }
}

class HTMLButton implements Button {
    public void render() { System.out.println("Web风格按钮"); }
}

特点

  • ✅ 符合开闭原则
  • ✅ 支持多态性
  • ❌ 类数量膨胀

三、抽象工厂模式(Abstract Factory)

核心思想:创建相关或依赖对象的家族,无需指定具体类
典型场景:跨平台UI组件、数据库访问套件

// 抽象工厂
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// 具体工厂(Windows风格)
class WinFactory implements GUIFactory {
    public Button createButton() { return new WinButton(); }
    public Checkbox createCheckbox() { return new WinCheckbox(); }
}

// 具体工厂(Mac风格)
class MacFactory implements GUIFactory {
    public Button createButton() { return new MacButton(); }
    public Checkbox createCheckbox() { return new MacCheckbox(); }
}

// 产品体系
interface Button { void paint(); }
interface Checkbox { void check(); }

class WinButton implements Button {
    public void paint() { System.out.println("Windows按钮"); }
}
class MacCheckbox implements Checkbox {
    public void check() { System.out.println("Mac复选框"); }
}

特点

  • ✅ 保证产品兼容性
  • ✅ 产品族一致性控制
  • ❌ 扩展新产品族困难

四、模板工厂模式(特殊实现)

混合模式:结合模板方法模式与工厂模式
典型实现

  1. 定义包含模板方法的抽象工厂
  2. 子类实现具体步骤
abstract class DocumentConverter {
    // 模板方法
    public final void convert() {
        loadDocument();
        parseContent();
        saveOutput();
    }

    abstract void loadDocument();
    abstract void parseContent();
    abstract void saveOutput();
}

class PDFConverter extends DocumentConverter {
    void loadDocument() { /* PDF加载逻辑 */ }
    void parseContent() { /* 解析PDF */ }
    void saveOutput() { /* 输出处理 */ }
}

适用场景:需要固定流程但步骤实现不同的场景


五、模式对比

模式

核心差异

最佳场景

简单工厂

集中式对象创建

创建逻辑简单的少量对象

工厂方法

子类决定对象类型

需要扩展单个产品类型

抽象工厂

创建产品家族

需要保证多产品兼容性