C++中的抽象工厂模式

devtools/2024/9/23 9:49:14/

目录

抽象工厂模式(Abstract Factory Pattern)

实际应用

跨平台GUI工厂

数据库访问抽象工厂

跨平台文件系统工厂

总结


抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式是一种创建型设计模式,它提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。抽象工厂模式通过定义多个创建方法,每个方法返回一种抽象产品类型的实例,来实现产品族的创建。

实际应用

1. 当一个系统要独立于它的产品的创建、组合和表示时。
2. 当一个系统要由多个产品系列中的一个来配置时。
3. 当需要提供一个产品类库,而只想显示它们的接口而不是实现时。

跨平台GUI工厂

在跨平台应用程序中,不同平台(例如:Windows、Mac、Linux)有不同的GUI组件。

#include <iostream>
#include <memory>// 抽象产品:按钮
class Button {
public:virtual void paint() = 0;virtual ~Button() = default;
};// 具体产品:Windows按钮
class WindowsButton : public Button {
public:void paint() override {std::cout << "Painting Windows Button" << std::endl;}
};// 具体产品:Mac按钮
class MacButton : public Button {
public:void paint() override {std::cout << "Painting Mac Button" << std::endl;}
};// 抽象产品:文本框
class TextBox {
public:virtual void draw() = 0;virtual ~TextBox() = default;
};// 具体产品:Windows文本框
class WindowsTextBox : public TextBox {
public:void draw() override {std::cout << "Drawing Windows TextBox" << std::endl;}
};// 具体产品:Mac文本框
class MacTextBox : public TextBox {
public:void draw() override {std::cout << "Drawing Mac TextBox" << std::endl;}
};// 抽象工厂
class GUIFactory {
public:virtual std::unique_ptr<Button> createButton() = 0;virtual std::unique_ptr<TextBox> createTextBox() = 0;virtual ~GUIFactory() = default;
};// 具体工厂:Windows GUI工厂
class WindowsFactory : public GUIFactory {
public:std::unique_ptr<Button> createButton() override {return std::make_unique<WindowsButton>();}std::unique_ptr<TextBox> createTextBox() override {return std::make_unique<WindowsTextBox>();}
};// 具体工厂:Mac GUI工厂
class MacFactory : public GUIFactory {
public:std::unique_ptr<Button> createButton() override {return std::make_unique<MacButton>();}std::unique_ptr<TextBox> createTextBox() override {return std::make_unique<MacTextBox>();}
};void clientCode(GUIFactory& factory) {auto button = factory.createButton();auto textBox = factory.createTextBox();button->paint();textBox->draw();
}int main() {std::unique_ptr<GUIFactory> factory = std::make_unique<WindowsFactory>();clientCode(*factory);factory = std::make_unique<MacFactory>();clientCode(*factory);return 0;
}

数据库访问抽象工厂

在企业应用中,不同的数据库(例如:MySQL、PostgreSQL、SQLite)需要由特定的工厂来创建相应的连接和命令对象。

#include <iostream>
#include <memory>// 抽象产品:数据库连接
class DBConnection {
public:virtual void connect() = 0;virtual ~DBConnection() = default;
};// 具体产品:MySQL连接
class MySQLConnection : public DBConnection {
public:void connect() override {std::cout << "Connecting to MySQL Database" << std::endl;}
};// 具体产品:PostgreSQL连接
class PostgreSQLConnection : public DBConnection {
public:void connect() override {std::cout << "Connecting to PostgreSQL Database" << std::endl;}
};// 抽象产品:数据库命令
class DBCommand {
public:virtual void execute() = 0;virtual ~DBCommand() = default;
};// 具体产品:MySQL命令
class MySQLCommand : public DBCommand {
public:void execute() override {std::cout << "Executing MySQL Command" << std::endl;}
};// 具体产品:PostgreSQL命令
class PostgreSQLCommand : public DBCommand {
public:void execute() override {std::cout << "Executing PostgreSQL Command" << std::endl;}
};// 抽象工厂
class DBFactory {
public:virtual std::unique_ptr<DBConnection> createConnection() = 0;virtual std::unique_ptr<DBCommand> createCommand() = 0;virtual ~DBFactory() = default;
};// 具体工厂:MySQL工厂
class MySQLFactory : public DBFactory {
public:std::unique_ptr<DBConnection> createConnection() override {return std::make_unique<MySQLConnection>();}std::unique_ptr<DBCommand> createCommand() override {return std::make_unique<MySQLCommand>();}
};// 具体工厂:PostgreSQL工厂
class PostgreSQLFactory : public DBFactory {
public:std::unique_ptr<DBConnection> createConnection() override {return std::make_unique<PostgreSQLConnection>();}std::unique_ptr<DBCommand> createCommand() override {return std::make_unique<PostgreSQLCommand>();}
};void clientCode(DBFactory& factory) {auto connection = factory.createConnection();auto command = factory.createCommand();connection->connect();command->execute();
}int main() {std::unique_ptr<DBFactory> factory = std::make_unique<MySQLFactory>();clientCode(*factory);factory = std::make_unique<PostgreSQLFactory>();clientCode(*factory);return 0;
}

跨平台文件系统工厂

在跨平台文件系统应用程序中,不同平台(例如:Windows、Linux、Mac)有不同的文件系统操作。

#include <iostream>
#include <memory>// 抽象产品:文件
class File {
public:virtual void open() = 0;virtual ~File() = default;
};// 具体产品:Windows文件
class WindowsFile : public File {
public:void open() override {std::cout << "Opening Windows File" << std::endl;}
};// 具体产品:Linux文件
class LinuxFile : public File {
public:void open() override {std::cout << "Opening Linux File" << std::endl;}
};// 抽象产品:目录
class Directory {
public:virtual void list() = 0;virtual ~Directory() = default;
};// 具体产品:Windows目录
class WindowsDirectory : public Directory {
public:void list() override {std::cout << "Listing Windows Directory" << std::endl;}
};// 具体产品:Linux目录
class LinuxDirectory : public Directory {
public:void list() override {std::cout << "Listing Linux Directory" << std::endl;}
};// 抽象工厂
class FileSystemFactory {
public:virtual std::unique_ptr<File> createFile() = 0;virtual std::unique_ptr<Directory> createDirectory() = 0;virtual ~FileSystemFactory() = default;
};// 具体工厂:Windows文件系统工厂
class WindowsFileSystemFactory : public FileSystemFactory {
public:std::unique_ptr<File> createFile() override {return std::make_unique<WindowsFile>();}std::unique_ptr<Directory> createDirectory() override {return std::make_unique<WindowsDirectory>();}
};// 具体工厂:Linux文件系统工厂
class LinuxFileSystemFactory : public FileSystemFactory {
public:std::unique_ptr<File> createFile() override {return std::make_unique<LinuxFile>();}std::unique_ptr<Directory> createDirectory() override {return std::make_unique<LinuxDirectory>();}
};void clientCode(FileSystemFactory& factory) {auto file = factory.createFile();auto directory = factory.createDirectory();file->open();directory->list();
}int main() {std::unique_ptr<FileSystemFactory> factory = std::make_unique<WindowsFileSystemFactory>();clientCode(*factory);factory = std::make_unique<LinuxFileSystemFactory>();clientCode(*factory);return 0;
}

总结

抽象工厂模式提供了一种创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。这个模式有助于提高系统的可扩展性和灵活性,使得系统能够在不同环境下使用不同的实现。


http://www.ppmy.cn/devtools/50894.html

相关文章

警示:AGI竞赛之未来十年

后新冠时代&#xff0c;人类智商普遍下降&#xff0c;人工智能赶超人类智能指日可待。 最近几天&#xff0c;AI领域悄悄流行一份AGI白皮书&#xff0c;虽然有些危言耸听&#xff0c;甚至包含以中国为竞争对手的阴谋论。下面是主要思想&#xff1a; 在过去的一年里&#xff1a…

dbForge Studioor MySQL v6 解锁版 安装教程(MYSQL数据库客户端)

前言 dbForge Studioor MySQL是一个在Windows平台被广泛使用的MySQL客户端&#xff0c;它能够使MySQL开发人员和管理人员在一个方便的环境中与他人一起完成创建和执行查询&#xff0c;开发和调试MySQL程序&#xff0c;自动化管理MySQL数据库对象等工作。 一、下载地址 下载链…

镜舟科技与喆塔科技签署战略合作协议,共拓工业领域数据应用

近日&#xff0c;镜舟科技与喆塔科技正式签署战略合作协议&#xff0c;旨在通过双方的深度合作&#xff0c;共同推动工业领域企业级数据分析与智能制造的融合创新&#xff0c;携手为行业客户提供更加精准、高效的数据驱动解决方案。 镜舟科技 CEO 孙文现与喆塔科技 CEO 赵文政出…

JUnit 5学习笔记

JUnit 5 学习笔记 1.JUnit5的改变2.JUnit5常用注解及测试2.1 DisplayName/Disabled/BeforeEach/AfterEach/BeforeAll/AfterAll2.2 Timeout2.3 RepeatedTest 3.断言3.1 简单断言3.2 数组断言3.3 组合断言3.4 异常断言3.5 超时断言3.6 快速失败 4.前置条件5.嵌套测试6.参数化测试…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] CPU算力分配(100分) - 三语言AC题解(Python/Java/Cpp)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 💻 ACM银牌🥈| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 📎在线评测链接 CPU算力分配(100分) 🌍 评测功能需要订阅专栏后私信联系清…

python如何对list求和

如何在Python中对多个list的对应元素求和&#xff0c;前提是每个list的长度一样。比如&#xff1a;a[1&#xff0c;2&#xff0c;3]&#xff0c;b[2&#xff0c;3&#xff0c;4]&#xff0c;c[3&#xff0c;4&#xff0c;5]&#xff0c;对a&#xff0c;b&#xff0c;c的对应元素…

记录一次CTF图片拼图安装工具montage+gaps成功步骤以及踩坑全过程

安装图片拼接工具montage&#xff1a; 1.安装 使用pip install montage无法安装montage工具的师傅可以尝试下面的方法 #Debian apt-get install graphicsmagick-imagemagick-compat#Ubuntu apt-get install graphicsmagick-imagemagick-compat#Alpine apk add imagemagick6#…

CCAA质量管理【学习笔记】​​ 备考知识点笔记(五)质量设计方法与工具

第五节 质量设计方法与工具 1 任 务 分 解 法 1.1 概念 任务分解法&#xff0c;又称工作分解结构 (Work Breakdown Structure, 简 称 WBS) 。WBS 指以可交付成果为 导向&#xff0c;对项目团队为实现项目目标并完成规定的可交付成果而执行的工作所进行的层次分解。W…