1. 抽象工厂模式概述
抽象工厂模式是创建型设计模式中的一种高级形态,它提供了一种封装一组具有共同主题但实现细节不同的工厂的方式。简单来说,抽象工厂就是"工厂的工厂",它能够创建一系列相关或依赖的对象,而无需指定它们的具体类。
我第一次在实际项目中应用抽象工厂模式是在开发一个跨平台的UI组件库时。当时需要为Windows、Mac和Linux三大操作系统提供风格统一的按钮、文本框等控件,但每个平台下的具体实现又完全不同。抽象工厂完美解决了这个问题——它让我能够定义一个创建UI控件的通用接口,然后为每个平台实现具体的工厂类。
提示:当你的系统需要创建多个产品族(比如不同风格的UI组件、不同数据库的访问对象),而这些产品族又需要在不同环境下切换时,抽象工厂就是最佳选择。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 抽象工厂模式结构解析
2.1 核心角色组成
抽象工厂模式包含以下关键角色:
- 抽象工厂(AbstractFactory):声明创建一系列产品对象的接口
- 具体工厂(ConcreteFactory):实现抽象工厂接口,创建具体的产品对象
- 抽象产品(AbstractProduct):声明产品对象的接口
- 具体产品(ConcreteProduct):实现抽象产品接口的具体类
- 客户端(Client):仅使用抽象工厂和抽象产品声明的接口
2.2 UML类图示例
java复制// 抽象产品A
interface Button {
void render();
}
// 具体产品A1
class WindowsButton implements Button {
public void render() {
System.out.println("渲染Windows风格按钮");
}
}
// 具体产品A2
class MacButton implements Button {
public void render() {
System.out.println("渲染Mac风格按钮");
}
}
// 抽象产品B
interface TextBox {
void display();
}
// 具体产品B1
class WindowsTextBox implements TextBox {
public void display() {
System.out.println("显示Windows风格文本框");
}
}
// 具体产品B2
class MacTextBox implements TextBox {
public void display() {
System.out.println("显示Mac风格文本框");
}
}
// 抽象工厂
interface GUIFactory {
Button createButton();
TextBox createTextBox();
}
// 具体工厂1
class WindowsFactory implements GUIFactory {
public Button createButton() {
return new WindowsButton();
}
public TextBox createTextBox() {
return new WindowsTextBox();
}
}
// 具体工厂2
class MacFactory implements GUIFactory {
public Button createButton() {
return new MacButton();
}
public TextBox createTextBox() {
return new MacTextBox();
}
}
// 客户端代码
class Application {
private Button button;
private TextBox textBox;
public Application(GUIFactory factory) {
button = factory.createButton();
textBox = factory.createTextBox();
}
public void render() {
button.render();
textBox.display();
}
}
3. 抽象工厂模式实战应用
3.1 跨平台UI开发案例
让我们更深入地看看如何在实际项目中应用抽象工厂模式。假设我们正在开发一个需要在Windows和MacOS上运行的图形编辑器:
java复制// 抽象产品:图形工具
interface ShapeTool {
void draw();
void erase();
}
// Windows版图形工具
class WindowsShapeTool implements ShapeTool {
public void draw() {
System.out.println("使用GDI+绘制图形");
}
public void erase() {
System.out.println("使用GDI+擦除图形");
}
}
// Mac版图形工具
class MacShapeTool implements ShapeTool {
public void draw() {
System.out.println("使用Quartz绘制图形");
}
public void erase() {
System.out.println("使用Quartz擦除图形");
}
}
// 抽象工厂
interface GraphicsFactory {
ShapeTool createShapeTool();
// 可以添加其他相关产品创建方法
}
// Windows工厂
class WindowsGraphicsFactory implements GraphicsFactory {
public ShapeTool createShapeTool() {
return new WindowsShapeTool();
}
}
// Mac工厂
class MacGraphicsFactory implements GraphicsFactory {
public ShapeTool createShapeTool() {
return new MacShapeTool();
}
}
3.2 数据库访问层设计
另一个经典应用场景是数据库访问层的设计。不同的数据库(MySQL、Oracle、SQL Server)有着不同的连接、命令等实现:
java复制// 抽象产品:数据库连接
interface Connection {
void connect();
void disconnect();
}
// 抽象产品:数据库命令
interface Command {
void execute(String query);
}
// MySQL具体产品
class MySQLConnection implements Connection {
public void connect() {
System.out.println("建立MySQL连接");
}
public void disconnect() {
System.out.println("关闭MySQL连接");
}
}
class MySQLCommand implements Command {
public void execute(String query) {
System.out.println("执行MySQL查询: " + query);
}
}
// Oracle具体产品
class OracleConnection implements Connection {
public void connect() {
System.out.println("建立Oracle连接");
}
public void disconnect() {
System.out.println("关闭Oracle连接");
}
}
class OracleCommand implements Command {
public void execute(String query) {
System.out.println("执行Oracle查询: " + query);
}
}
// 抽象工厂
interface DatabaseFactory {
Connection createConnection();
Command createCommand();
}
// MySQL工厂
class MySQLFactory implements
