C++25设计模式之抽象工厂模式

C++25设计模式之抽象工厂模式_第1张图片
图片引用于菜鸟教程

简介:抽象工厂模式是为了创建一系列产品而提供的解决方案,和简单工厂模式不一样的是,抽象工厂模式不只是创建一种产品,而是创建产品的一系列对象

优点:扩展性高,可以通过一系列对象实现某个功能

缺点:一旦添加新产品,就需要再新增一个工厂类

例子背景:现在客户需要i3、i5、i7、i9、DIY电脑主机,而这些主机各方面组件并不相同,那么使用抽象工厂模式,在为各种主机组装时就无需制定配件的型号,它会自动根据产品自动生成需要的配件

概念:抽象工厂模式提供某个具体产品的创建,每一个工厂只负责某个对象的创建

抽象工厂模式代码:

  • 抽象产品类
#pragma once

class IProduct
{
public:
        virtual ~IProduct(){}
        virtual void CreateCpu() = 0;
        virtual void CreateMainboard() = 0;
        virtual void CreateGraphicscard() = 0;
        virtual void CreateMemorybank() = 0;
        virtual void CreateHarddisk() = 0;
        virtual void CreatePowersupply() = 0;
        virtual void CreateHeatdissipation() = 0;
        virtual void CreateComputercase() = 0;
        virtual void CreateComputerAccessories()
        {
                CreateCpu();
                CreateMainboard();
                CreateGraphicscard();
                CreateMemorybank();
                CreateHarddisk();
                CreatePowersupply();
                CreateHeatdissipation();
                CreateComputercase();
        }
};
  • 具体产品类
#pragma once
#include "IProduct.h"
#include 

class i3Hostparts : public IProduct
{
public:
        ~i3Hostparts(){}
        virtual void CreateCpu()
        {
                printf("创建了一个12代i3Cpu\n");
        }
        virtual void CreateMainboard()
        {
                printf("创建了一个h510主板\n");
        }
        virtual void CreateGraphicscard()
        {
                printf("创建了一个3050Ti显卡\n");
        }
        virtual void CreateMemorybank()
        {
                printf("创建了一个8G内存\n");
        }
        virtual void CreateHarddisk()
        {
                printf("创建了一个250G M.2硬盘\n");
        }
        virtual void CreatePowersupply()
        {
                printf("创建了一个550w电源\n");
        }
        virtual void CreateHeatdissipation()
        {
                printf("创建了一个风冷散热\n");
        }
        virtual void CreateComputercase()
        {
                printf("创建了一个爱国者暗影机箱\n");
        }
};

class i5Hostparts : public IProduct
{
public:
        ~i5Hostparts(){}
        virtual void CreateCpu()
        {
                printf("创建了一个12代i5Cpu\n");
        }
        virtual void CreateMainboard()
        {
                printf("创建了一个b560主板\n");
        }
        virtual void CreateGraphicscard()
        {
                printf("创建了一个3060Ti显卡\n");
        }
        virtual void CreateMemorybank()
        {
                printf("创建了一个8G内存\n");
        }
        virtual void CreateHarddisk()
        {
                printf("创建了一个250G M.2硬盘\n");
        }
        virtual void CreatePowersupply()
        {
                printf("创建了一个650w电源\n");
        }
        virtual void CreateHeatdissipation()
        {
                printf("创建了一个风冷散热\n");
        }
        virtual void CreateComputercase()
        {
                printf("创建了一个复仇者机箱\n");
        }
};

class i7Hostparts : public IProduct
{
public:
        ~i7Hostparts(){}
        virtual void CreateCpu()
        {
                printf("创建了一个12代i7Cpu\n");
        }
        virtual void CreateMainboard()
        {
                printf("创建了一个z590主板\n");
        }
        virtual void CreateGraphicscard()
        {
                printf("创建了一个3070Ti显卡\n");
        }
        virtual void CreateMemorybank()
        {
                printf("创建了一个16G内存\n");
        }
        virtual void CreateHarddisk()
        {
                printf("创建了一个500G M.2硬盘\n");
        }
        virtual void CreatePowersupply()
        {
                printf("创建了一个700w电源\n");
        }
        virtual void CreateHeatdissipation()
        {
                printf("创建了一个240水冷散热\n");
        }
        virtual void CreateComputercase()
        {
                printf("创建了一个玩嘉机箱\n");
        }
};

class i9Hostparts : public IProduct
{
public:
        ~i9Hostparts(){}
        virtual void CreateCpu()
        {
                printf("创建了一个12代i9Cpu\n");
        }
        virtual void CreateMainboard()
        {
                printf("创建了一个z690-e主板\n");
        }
        virtual void CreateGraphicscard()
        {
                printf("创建了一个3090Ti显卡\n");
        }
        virtual void CreateMemorybank()
        {
                printf("创建了一个32G内存\n");
        }
        virtual void CreateHarddisk()
        {
                printf("创建了一个1T M.2硬盘\n");
        }
        virtual void CreatePowersupply()
        {
                printf("创建了一个1200w电源\n");
        }
        virtual void CreateHeatdissipation()
        {
                printf("创建了一个360水冷散热\n");
        }
        virtual void CreateComputercase()
        {
                printf("创建了一个太阳神机箱\n");
        }
};
  • 抽象工厂类
#pragma once

class IFactory
{
public:
        virtual ~IFactory() {}
        virtual void AssembleTheHost() = 0;
};
  • 具体工厂类
#pragma once
#include "IFactory.h"
#include "Hostparts.h"
#include 
#include 

class i3AssemblyFactory : IFactory
{
public:
        ~i3AssemblyFactory(){}
        virtual void AssembleTheHost()
        {
                IProduct* i3 = new i3Hostparts;
                i3->CreateComputerAccessories();
                printf("组装i3主机完成!\n");
        }
};

class i5AssemblyFactory : IFactory
{
public:
        ~i5AssemblyFactory(){}
        virtual void AssembleTheHost()
        {
                IProduct* i5 = new i5Hostparts;
                i5->CreateComputerAccessories();
                printf("组装i5主机完成!\n");
        }
};

class i7AssemblyFactory : IFactory
{
public:
        ~i7AssemblyFactory(){}
        virtual void AssembleTheHost()
        {
                IProduct* i7 = new i7Hostparts;
                i7->CreateComputerAccessories();
                printf("组装i7主机完成!\n");
        }
};

class i9AssemblyFactory : IFactory
{
public:
        ~i9AssemblyFactory(){}
        virtual void AssembleTheHost()
        {
                IProduct* i9 = new i9Hostparts;
                i9->CreateComputerAccessories();
                printf("组装i9主机完成!\n");
        }
};


class DiyAssemblyFactory : IFactory
{
public:
        typedef std::function<void(void)> func;
private:
        std::vector<func> m_diyAccessories;
public:
        ~DiyAssemblyFactory(){}
        virtual void AssembleTheHost()
        {
                for (auto iter = m_diyAccessories.begin(); iter != m_diyAccessories.end(); ++iter)
                {
                        (*iter)();
                }
                printf("组装Diy主机完成!\n");
        }
        void addDiyAccessories(func fc)
        {
                m_diyAccessories.push_back(fc);
        }
};
  • 调用者
#include "HostpartsFactory.h"
#include 

int main()
{
        std::shared_ptr<i3AssemblyFactory> i3factory(new i3AssemblyFactory);
        i3factory->AssembleTheHost();
        std::shared_ptr<i5AssemblyFactory> i5factory(new i5AssemblyFactory);
        i5factory->AssembleTheHost();
        std::shared_ptr<i7AssemblyFactory> i7factory(new i7AssemblyFactory);
        i7factory->AssembleTheHost();
        std::shared_ptr<i9AssemblyFactory> i9factory(new i9AssemblyFactory);
        i9factory->AssembleTheHost();

        std::shared_ptr<IProduct> i3HostPart(new i3Hostparts);
        std::shared_ptr<IProduct> i5HostPart(new i5Hostparts);
        std::shared_ptr<IProduct> i7HostPart(new i7Hostparts);
        std::shared_ptr<IProduct> i9HostPart(new i9Hostparts);

        std::shared_ptr<DiyAssemblyFactory> diyfactory(new DiyAssemblyFactory);
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateCpu, i5HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateMainboard, i7HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateGraphicscard, i7HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateMemorybank, i7HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateHarddisk, i9HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreatePowersupply, i9HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateHeatdissipation, i9HostPart.get()));
        diyfactory->addDiyAccessories(std::bind(&IProduct::CreateComputercase, i9HostPart.get()));
        diyfactory->AssembleTheHost();
        getchar();
        return 0;
}

总结:
抽象工厂模式(Abstract Factory):是工厂方法模式的进一步延伸,它提供了功能更为强大的工厂类并且具备较好的可扩展性

PS:
其实最后的diy已经不属于抽象工厂模式范围内了,只是我随手写的,看看就好了。

作者:丶梦爱
博客:https://blog.csdn.net/u014732297(转载请说明出处)

你可能感兴趣的:(C++设计模式25种,visual,studio,c++,抽象工厂模式)