设计模式——抽象工厂模式

抽象工厂模式(Abstract Factory)

抽象工厂模式是一种创建型设计模式 ,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

c++ 复制代码
#include <iostream>
#include <string>

using namespace std;

// 产品系列中的每个不同产品都应有一个基本接口。产品的所有变体都必须实现此接口。
class AbstractProductA
{
public:
    virtual ~AbstractProductA(){};
    virtual string UsefulFunctionA() const = 0;
};

// 具体产品是由相应的具体工厂创建的
class ConcreteProductA1 : public AbstractProductA
{
public:
    string UsefulFunctionA() const override
    {
        return "The result of the product A1";
    }
};

class ConcreteProductA2 : public AbstractProductA
{
public:
    string UsefulFunctionA() const override
    {
        return "The result of the product A2";
    }
};

// 另一个产品的基本接口
// 只有同一类的产品之间才可以有适当的交互
class AbstractProductB
{
public:
    virtual ~AbstractProductB() {}
    virtual string UsefulFunctionB() const = 0;

    // 抽象工厂确保其创建的所有产品都是同一变体,因此兼容
    virtual string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const = 0;
};

class ConcreteProductB1 : public AbstractProductB
{
public:
    string UsefulFunctionB() const override
    {
        return "The result of the product B1";
    }

    // 变体 Product B1 只能与变体 Product A1 正确配合使用。不过,它接受 AbstractProductA 的任何实例作为参数。
    string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override
    {
        const string result = collaborator.UsefulFunctionA();
        return "The result of the B1 collaborating with ( " + result + " )";
    }
};

class ConcreteProductB2 : public AbstractProductB
{
public:
    string UsefulFunctionB() const override
    {
        return "The result of the product B2";
    }

    // 变体 Product B2 只能与变体 Product A2 正确配合使用。不过,它接受 AbstractProductA 的任何实例作为参数。
    string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override
    {
        const string result = collaborator.UsefulFunctionA();
        return "The result of the B2 collaborating with ( " + result + " )";
    }
};

// 抽象工厂接口声明了一组返回不同抽象产品的方法,这些产品属于一个系列,一个系列的产品可以相互协作。
// 一个产品系列可能有多个变体,但是一个变体的产品与另一个变体的产品不兼容
class AbstractFactory
{
public:
    virtual AbstractProductA *CreateProductA() const = 0;
    virtual AbstractProductB *CreateProductB() const = 0;
};

// 具体的工厂生产属于单一变体的一系列产品
// 具体工厂方法返回的是抽象产品,而在方法内部实例化一个具体产品
class ConcreteFactory1 : public AbstractFactory
{
public:
    AbstractProductA *CreateProductA() const override
    {
        return new ConcreteProductA1();
    }

    AbstractProductB *CreateProductB() const override
    {
        return new ConcreteProductB1();
    }
};

class ConcreteFactory2 : public AbstractFactory {
public:
    AbstractProductA *CreateProductA() const override {
        return new ConcreteProductA2();
    }
    AbstractProductB *CreateProductB() const override {
        return new ConcreteProductB2();
    }
};

void ClientCode(const AbstractFactory &factory) {
    const AbstractProductA *product_a = factory.CreateProductA();
    const AbstractProductB *product_b = factory.CreateProductB();
    std::cout << product_b->UsefulFunctionB() << "\n";
    std::cout << product_b->AnotherUsefulFunctionB(*product_a) << "\n";
    delete product_a;
    delete product_b;
}

int main() {
    std::cout << "Client: Testing client code with the first factory type:\n";
    ConcreteFactory1 *f1 = new ConcreteFactory1();
    ClientCode(*f1);
    delete f1;
    std::cout << std::endl;
    std::cout << "Client: Testing the same client code with the second factory type:\n";
    ConcreteFactory2 *f2 = new ConcreteFactory2();
    ClientCode(*f2);
    delete f2;
    return 0;
}

输出为

c++ 复制代码
Client: Testing client code with the first factory type:
The result of the product B1
The result of the B1 collaborating with ( The result of the product A1 )

Client: Testing the same client code with the second factory type:
The result of the product B2
The result of the B2 collaborating with ( The result of the product A2 )
相关推荐
geovindu1 小时前
rust: Template Method Pattern
开发语言·后端·设计模式·rust·行为模式·模板方法模
geovindu3 小时前
rust: Template Method Pattern(续)
开发语言·后端·设计模式·rust·行为模式·板方法模式
博风4 小时前
17、设计模式应用举例:单件模式:多线程争冠军
设计模式
高晶17 小时前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
高晶17 小时前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
延晗17 小时前
【从零开始 DeepSeek Harness 插件开发教程】二、一切皆插件:简单理解一下 Cordis 内核与 DSH 心智模型
设计模式·deepseek
高晶18 小时前
Android 通讯录应用开发实战:从零搭建 SQLite 数据持久化完整项目
android·设计模式·前端框架
日和雫1 天前
封装、继承、多态 & 组合,为什么组合大于继承?
设计模式
代钦塔拉1 天前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式
haluhalu.1 天前
深入理解AIAgent(1.3)--- 设计模式
人工智能·python·设计模式·langchain