天天看點

C++抽象工廠模式+代碼實作

        • 抽象工廠模式(Abstract Factory )
        • 抽象工廠模式實作:
        • 精髓在這裡:

抽象工廠模式(Abstract Factory )

抽象工廠模式為建立一組對象提供了一種解決方案。與工廠方法模式相比,抽象工廠模式中的具體工廠不隻是建立一種産品,它負責建立一族産品。

如下圖中的ConcreteFactory1是一系列産品,ConcreteFactory2則是另一系列産品

他們分别負責生産A1,B1和A2,B2

C++抽象工廠模式+代碼實作

圖檔引用自

圖檔作者:中南大學應用技術博士劉偉

抽象工廠模式實作:

Dell.h
#pragma once                                                                                                                                                  
class Dell
{
public:
    virtual void CPU() = ;
    virtual void Graphics() = ;
    virtual void Memory() = ;
};
           
HP.h
#pragma once
class HP
{
public:
    virtual void CPU() = ;
    virtual void Graphics() = ;
    virtual void Memory() = ;                                                                                                                                
};
           
NightElf.h
#pragma once
#include "HP.h"
class NightElf : public HP
{
public:
    void CPU(){
        std::cout<<"Intel i7-7700HQ"<<" ";
    }   
    void Graphics()
    {   
        std::cout<<"技嘉 GTX1060 6G"<<" ";
    }                                                                                                                                                         
    void Memory()
    {   
        std::cout<<"海盜船 複仇者 8G*2"<<std::endl;
    }   
};
           
ShadowElf.h
#pragma once
#include "HP.h"
class ShadowElf : public HP  
{
    public:
    void CPU(){
        std::cout<<"Intel i5-7300HQ"<<" ";
    }   
    void Graphics()
    {   
        std::cout<<"技嘉 GTX1070 3G"<<" ";
    }   
    void Memory()
    {   
        std::cout<<"Samsung 8G*2"<<std::endl;
    }   
};
           
Ranger.h
#pragma once                                                                                                                                                  
#include "Dell.h"
class Ranger : public Dell
{
    public:
        void CPU(){
            std::cout<<"Intel i7-8700HQ"<<" ";
        }   
        void Graphics()
        {   
            std::cout<<"技嘉 GTX1050 6G"<<" ";
        }   
        void Memory()
        {   
            std::cout<<"KingSton DDR4 8G*2"<<std::endl;
        }   
};
           
Laptop.h
#pragma once
#include "Dell.h"
#include "HP.h"
class Laptop
{
public:
    virtual Dell* BuildDell() = ;
    virtual HP* BuildHP() = ;                                                                                                                                
};
           

第一系列産品的工廠

LaptopFactory.h
#pragma once
#include "Laptop.h"
#include "Ranger.h"
#include "NightElf.h"
#include "ShadowElf.h"
class LaptopFactory : public Laptop
{

    public:
        Dell* BuildDell()
        {   
            std::cout<<"DELL遊匣"<<" ";
            Dell* pLaptop = new Ranger();
            return pLaptop;
        }   
        HP* BuildHP(string str)
        {   
            HP* pLaptop = nullptr;
            if(str == "NightElf")
            {   
                std::cout<<"HP暗夜精靈"<<" ";
                pLaptop = new NightElf();
            }   
            else
            {   
                std::cout<<"HP暗影精靈"<<" ";//此處其實可以用一個新工廠來生産該産品,例如:寫一個
                //Second_Laptop_factory來實作Laptop,完成暗影精靈的生産.這就是抽象工廠的精髓
                pLaptop = new ShadowElf(); 
            }   
            return pLaptop;
        }   
};
           
test.cpp
#include <iostream>
#include "LaptopFactory.h"

using namespace std;

int main()
{
    Laptop* laptop= new LaptopFactory();

    Dell* pDell_Laptop = laptop->BuildDell();                                                                                                                 
    pDell_Laptop->CPU();
    pDell_Laptop->Graphics();
    pDell_Laptop->Memory();

    HP* pHP_Laptop = laptop->BuildHP("NightElf");
    pHP_Laptop->CPU();
    pHP_Laptop->Graphics();
    pHP_Laptop->Memory();

    pHP_Laptop = laptop->BuildHP("ShadowElf");
    pHP_Laptop->CPU();
    pHP_Laptop->Graphics();
    pHP_Laptop->Memory();

    return ;
}
           

運作測試結果:

C++抽象工廠模式+代碼實作

精髓在這裡:

這是第二個工廠:負責生産第二系列産品

Second_Laptop_Factory.h
#pragma once
#include "Laptop.h"
#include "ShadowElf.h"
class Second_Laptop_Factory : public Laptop
{
    public:
        Dell* BuildDell()/*balabalabala*/
        {
            return NULL;
        }
        HP* BuildHP(/*string str*/)
        {
            HP* pLaptop = new ShadowElf();
            return pLaptop;
        }
};
           

以上代碼純手打,如若有誤請諒解

繼續閱讀