设计模式之抽象工厂模式--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;
}
相关推荐
王RuaRua1 小时前
VScode C/C++环境配置
c语言·c++·vscode
橘子131 小时前
Linux线程同步(四)
linux·c++
想想吴3 小时前
10. 引用计数
c++·引用计数
yolo_guo3 小时前
opencv 学习: 04 通过ROI处理图片局部数据,以添加水印为例
linux·c++·opencv
顺顺 尼3 小时前
模板进阶和array
c++
Damon小智4 小时前
鸿蒙元服务深度实践:跨端唤醒与状态共享的设计模式
华为·设计模式·harmonyos
shaominjin1234 小时前
单例模式:设计模式中的“独一无二“之道
android·单例模式·设计模式
一匹电信狗4 小时前
【牛客CM11】链表分割
c语言·开发语言·数据结构·c++·算法·leetcode·stl
困鲲鲲4 小时前
ROS2系列 (10) : C++话题通信节点——发布者示例
c++·ros2
..过云雨4 小时前
11.【Linux系统编程】文件系统详解——从磁盘硬件到文件系统
linux·c++·后端·缓存