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

目录

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

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

[三、抽象工厂模式(Abstract Factory)](#三、抽象工厂模式(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() { /
    输出处理 */ }
    }

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


五、模式对比

|------|----------|-------------|
| 模式 | 核心差异 | 最佳场景 |
| 简单工厂 | 集中式对象创建 | 创建逻辑简单的少量对象 |
| 工厂方法 | 子类决定对象类型 | 需要扩展单个产品类型 |
| 抽象工厂 | 创建产品家族 | 需要保证多产品兼容性 |

相关推荐
AOwhisky9 分钟前
Kubernetes 学习笔记:集群管理、命名空间与 Pod 基础
linux·运维·笔记·学习·云原生·kubernetes
小怪吴吴44 分钟前
idea 开发Android
android·java·intellij-idea
嘻嘻哈哈樱桃1 小时前
牛客经典101题题解集--动态规划
java·数据结构·python·算法·职场和发展·动态规划
一次旅行1 小时前
IDEA安装CC GUI新手指南
java·ide·intellij-idea
小龙在慢慢变强..1 小时前
目录结构(FHS 标准)
linux·运维·服务器
超梦dasgg1 小时前
Spring AI 智能航空助手项目实战
java·人工智能·后端·spring·ai编程
2035去旅行1 小时前
嵌入式开发,如何选择C标准库
linux·arm开发
刘延林.1 小时前
win11系统下通过 WSL2 安装Ubuntu 24.04 使用RTX 5080 GPU
linux·运维·ubuntu
星恒讯工业路由器1 小时前
星恒讯工业生产自动化解决方案
运维·物联网·自动化·智能路由器·信息与通信
a8a3021 小时前
Laravel9.x新特性全解析
运维·spring boot·nginx