设计模式之抽象工厂模式--c++

3. 抽象工厂 -- 产品族,创建系列

简介

抽象工厂模式(Abstract Factory Pattern)隶属于设计模式中的创建型模式,用于产品族的构建。抽象工厂是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂是指当有多个抽象角色时使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体情况下,创建多个产品族中的产品对象。 [1]

工厂模式中的每一个形态都是针对一定问题的解决方案,工厂方法针对的是多个产品系列结构;而抽象工厂模式针对的是多个产品族结构,一个产品族内有多个产品系列。抽象工厂模式_百度百科 (baidu.com)

使用场景

适合不同系列的产品工厂

实现
cpp 复制代码
// 抽象工厂模式
// 时间:2023-10-28
// 作者:@conceal
#include <iostream>
#include <string>
using namespace std;

// 创建一个制造笔的接口
class Pen
{
public:
    virtual void manufacture() = 0;
};

// 创建一个制造铅笔的类
class Pencil : public Pen
{
public:
    void manufacture()
    {
        cout << "制造铅笔" << endl;
    }
};

// 创建一个制造圆珠笔的类
class BallPen : public Pen
{
public:
    void manufacture()
    {
        cout << "制造圆珠笔" << endl;
    }
};

// 创建一个制造钢笔的类
class FountainPen : public Pen
{
public:
    void manufacture()
    {
        cout << "制造钢笔" << endl;
    }
};

// 创建一个制造笔的工厂
class PenFactory
{
public:
    Pen *createPen(string penType)
    {
        if (penType == "Pencil")
        {
            return new Pencil();
        }
        else if (penType == "BallPen")
        {
            return new BallPen();
        }
        else if (penType == "FountainPen")
        {
            return new FountainPen();
        }
        else
        {
            return NULL;
        }
    }
};

// 创建一个制造笔芯的接口
class Refill
{
public:
    virtual void manufacture() = 0;
};

// 创建一个制造铅笔芯的类
class PencilRefill : public Refill
{
public:
    void manufacture()
    {
        cout << "制造铅笔芯" << endl;
    }
};

// 创建一个制造圆珠笔芯的类
class BallPenRefill : public Refill
{
public:
    void manufacture()
    {
        cout << "制造圆珠笔芯" << endl;
    }
};

// 创建一个制造钢笔芯的类
class FountainPenRefill : public Refill
{
public:
    void manufacture()
    {
        cout << "制造钢笔芯" << endl;
    }
};

// 创建一个制造笔芯的工厂
class RefillFactory
{
public:
    Refill *createRefill(string refillType)
    {
        if (refillType == "PencilRefill")
        {
            return new PencilRefill();
        }
        else if (refillType == "BallPenRefill")
        {
            return new BallPenRefill();
        }
        else if (refillType == "FountainPenRefill")
        {
            return new FountainPenRefill();
        }
        else
        {
            return NULL;
        }
    }
};

// 创建一个制造笔和笔芯的工厂
class FactoryProducer
{
public:
    static PenFactory *getPenFactory()
    {
        return new PenFactory();
    }
    static RefillFactory *getRefillFactory()
    {
        return new RefillFactory();
    }
};

int main()
{
    PenFactory *penFactory = FactoryProducer::getPenFactory();
    Pen *pencil = penFactory->createPen("Pencil");
    pencil->manufacture();
    Pen *ballPen = penFactory->createPen("BallPen");
    ballPen->manufacture();
    Pen *fountainPen = penFactory->createPen("FountainPen");
    fountainPen->manufacture();

    RefillFactory *refillFactory = FactoryProducer::getRefillFactory();
    Refill *pencilRefill = refillFactory->createRefill("PencilRefill");
    pencilRefill->manufacture();
    Refill *ballPenRefill = refillFactory->createRefill("BallPenRefill");
    ballPenRefill->manufacture();
    Refill *fountainPenRefill = refillFactory->createRefill("FountainPenRefill");
    fountainPenRefill->manufacture();

    return 0;
}
相关推荐
山登绝顶我为峰 3(^v^)314 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
十五年专注C++开发3 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
QuantumStack5 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6735 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
缘来是庄5 小时前
设计模式之访问者模式
java·设计模式·访问者模式
whoarethenext6 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
Jay_5157 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
xiaolang_8616_wjl8 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
FrostedLotus·霜莲8 小时前
C++主流编辑器特点比较
开发语言·c++·编辑器
hqxstudying8 小时前
Java创建型模式---单例模式
java·数据结构·设计模式·代码规范