设计模式之抽象工厂模式--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;
}
相关推荐
顾子茵2 分钟前
c++从入门到精通(五)--异常处理,命名空间,多继承与虚继承
开发语言·c++
软考真题app1 小时前
软件设计师考试结构型设计模式考点全解析
设计模式·软件设计师·结构型设计模式·考试考点
YueiL1 小时前
基于RK3588的智慧农场系统开发|RS485总线|华为云IOT|node-red|MQTT
c++·物联网·华为云·rk3588·rs485
二进制人工智能1 小时前
【OpenGL学习】(二)OpenGL渲染简单图形
c++·opengl
Dream it possible!2 小时前
LeetCode 热题 100_寻找重复数(100_287_中等_C++)(技巧)(暴力解法;哈希集合;二分查找)
c++·leetcode·哈希算法
丶Darling.3 小时前
Day119 | 灵神 | 二叉树 | 二叉树的最近共公共祖先
数据结构·c++·算法·二叉树
醍醐三叶4 小时前
C++文件操作--2 二进制文件操作
开发语言·c++
li星野4 小时前
C++:C++内存管理
开发语言·c++
溟洵4 小时前
【C++ Qt】布局管理器
开发语言·c++·qt
我家大宝最可爱4 小时前
c++动态链接库
开发语言·c++