设计模式之抽象工厂模式--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;
}
相关推荐
AI+程序员在路上26 分钟前
Qt6中模态与非模态对话框区别
开发语言·c++·qt
岁忧5 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
蜉蝣之翼❉10 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae11 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
lixzest11 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
ffcf12 小时前
设计模式—专栏简介
设计模式
_Chipen12 小时前
C++基础问题
开发语言·c++
灿烂阳光g12 小时前
OpenGL 2. 着色器
c++·opengl
AA陈超13 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
mit6.82414 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端