设计模式之抽象工厂

目录

前言

一、模式关键点

二、应用场景

三、具体案例


前言

抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以使客户端在不必指定产品的具体情况下,创建多个产品族中的产品对象

一、模式关键点

抽象工厂:是工厂方法的核心,任何在该模式中创建的对象的工厂类必须实现这个接口。

具体工厂:实现抽象工厂接口的具体工厂类,被调用以创建具体的产品对象。

抽象产品:具体产品的共同父类或共同拥有的接口。

具体产品:实现抽象产品角色所定义的接口,某具体产品由具体工厂创建

二、应用场景

在不指定具体产品的情况下,创建多个产品族中的产品对象

三、具体案例

比较经典的工厂生产水果蔬菜案例,水果包括苹果、香蕉、桃子,蔬菜包括土地、番茄、紫菜

产品类文件

#ifndef PRODUCT_H
#define PRODUCT_H


//水果产品基类
class AFruits
{
public:
    virtual QString name()=0;
};
//蔬菜产品基类
class AVegetables
{
public:
    virtual QString name()=0;
};

//具体水果产品
class Apple : public AFruits
{
public:
    virtual QString name()
    {
        return "This is Apple";
    }
};

class Banana : public AFruits
{
public:
    virtual QString name()
    {
        return "this is Banana";
    }
};

class Peach : public AFruits
{
public:
    virtual QString name()
    {
        return "this is Peach";
    }
};
//具体蔬菜产品
class Potato : public AVegetables
{
public:
    virtual QString name()
    {
        return "this is Potato";
    }
};

class Tomato : public AVegetables
{
public:
    virtual QString name()
    {
        return "this is Tomato";
    }
};

class Laver : public AVegetables
{
public:
    virtual QString name()
    {
        return "this is Laver";
    }
};
#endif // PRODUCT_H

工厂类文件

#ifndef FACTORY_H
#define FACTORY_H

#include "product.h"

//抽象工厂基类
class AFactory
{
public:
    enum FACTORY_TYPE
    {
        FACTPRY_A,
        FACTORY_B,
        FACTORY_C
    };
    virtual AFruits * CreateFruits()=0;
    virtual AVegetables * CreateVegetables()=0;
    static AFactory * CreateFactory(FACTORY_TYPE type);
};

//具体工厂类
class A_Factory : public AFactory
{
public:
    virtual AFruits *CreateFruits()
    {
        return new Apple();
    }

    virtual AVegetables * CreateVegetables()
    {
        return new Potato();
    }
};


class B_Factory : public AFactory
{
public:
    virtual AFruits *CreateFruits()
    {
        return new Banana();
    }

    virtual AVegetables * CreateVegetables()
    {
        return new Tomato();
    }
};

class C_Factory : public AFactory
{
public:
    virtual AFruits *CreateFruits()
    {
        return new Peach();
    }

    virtual AVegetables * CreateVegetables()
    {
        return new Laver();
    }
};
//创建具体的工厂
AFactory * AFactory::CreateFactory(FACTORY_TYPE type)
{
    AFactory * pFactory=nullptr;

    switch (type) {
    case FACTPRY_A:
        pFactory=new A_Factory();
        break;
    case FACTORY_B:
        pFactory=new B_Factory();
        break;
    case FACTORY_C:
        pFactory=new C_Factory();
        break;
    default:
        break;
    }

    return pFactory;
}

#endif // FACTORY_H

客户端使用

#include <QCoreApplication>
#include <QDebug>
#include "factory.h"

#ifndef POINT_DELETE
#define POINT_DELETE(p) {if(p){delete(p);(p)=nullptr;}}
#endif

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //A工厂
    AFactory * pFactory = AFactory::CreateFactory(AFactory::FACTPRY_A);
    AFruits *pFruits = pFactory->CreateFruits();
    AVegetables *pVegetables=pFactory->CreateVegetables();

    qDebug() << pFruits->name()<<endl;
    qDebug() << pVegetables->name()<<endl;

    POINT_DELETE(pFactory);
    POINT_DELETE(pFruits);
    POINT_DELETE(pVegetables);

    //B工厂
    pFactory = AFactory::CreateFactory(AFactory::FACTORY_B);
    pFruits=pFactory->CreateFruits();
    pVegetables=pFactory->CreateVegetables();

    qDebug() << pFruits->name()<<endl;
    qDebug() << pVegetables->name()<<endl;

    POINT_DELETE(pFactory);
    POINT_DELETE(pFruits);
    POINT_DELETE(pVegetables);

    //C工厂
    pFactory = AFactory::CreateFactory(AFactory::FACTORY_C);
    pFruits=pFactory->CreateFruits();
    pVegetables=pFactory->CreateVegetables();

    qDebug() << pFruits->name()<<endl;
    qDebug() << pVegetables->name()<<endl;

    POINT_DELETE(pFactory);
    POINT_DELETE(pFruits);
    POINT_DELETE(pVegetables);

    return a.exec();
}

输出结果如下图


 

总结

抽象工厂模式应用比较普遍,本实例在Qt Creator中创建,应用QT的控制台程序,编译器使用的mingw

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值