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

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

简介

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

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

使用场景

适合不同系列的产品工厂

实现
// 抽象工厂模式
// 时间: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值