当前位置:Gxlcms > mysql > 【2】设计模式之抽象工厂模式(AbstractFactory)

【2】设计模式之抽象工厂模式(AbstractFactory)

时间:2021-07-01 10:21:17 帮助过:22人阅读

注释: main(),女娲 IHuman,产品接口 CYellowHuman,抽象产品之一 CYellowFemaleHuman,具体产品之一 CYellowMaleHuman,具体产品之二 CWhiteHuman,抽象产品之二 CWhiteFemaleHuman,具体产品之三 CWhiteMaleHuman,具体产品之四 CBlackHuman,抽象产品之

注释:

main(),女娲

IHuman,产品接口

CYellowHuman,抽象产品之一

CYellowFemaleHuman,具体产品之一

CYellowMaleHuman,具体产品之二

CWhiteHuman,抽象产品之二

CWhiteFemaleHuman,具体产品之三

CWhiteMaleHuman,具体产品之四

CBlackHuman,抽象产品之三

CBlackFemaleHuman,具体产品之五

CBlackMaleHuman,具体产品之六

IHumanFactory,抽象工厂

CStandardHumanFactory,抽象工厂基类(此类可有可无)

CFemaleHumanFactory,工厂之一

CMaleHumanFactory,工厂之二

工程目录:


3、代码分析

1)Man-White

(1)WhiteHuman

//WhiteHuman.h
#ifndef __AbstractFactory__WhiteHuman__
#define __AbstractFactory__WhiteHuman__

#include "IHuman.h"

class CWhiteHuman : public IHuman
{
public:
    CWhiteHuman(void);
    ~CWhiteHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__WhiteHuman__) */

//WhiteHuman.cpp
#include "WhiteHuman.h"
#include 
using std::cout;
using std::endl;

CWhiteHuman::CWhiteHuman(void)
{
    
}
CWhiteHuman::~CWhiteHuman(void)
{
    
}
void CWhiteHuman::Cry()
{
    cout << "白种人哭" << endl;
}
void CWhiteHuman::Laugh()
{
    cout << "白种人笑" << endl;
}
void CWhiteHuman::Talk()
{
    cout << "白种人说" << endl;
}
(2)WhiteMaleHuman

//WhiteMaleHuman.h
#ifndef __AbstractFactory__WhiteMaleHuman__
#define __AbstractFactory__WhiteMaleHuman__

#include 
#include "WhiteHuman.h"
using std::cout;
using std::endl;

class CWhiteMaleHuman : public CWhiteHuman
{
public:
    CWhiteMaleHuman(void)
    {
        
    }
    ~CWhiteMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个男性白种人" << endl;
    }
};

#endif /* defined(__AbstractFactory__WhiteMaleHuman__) */

//WhiteMaleHuman.cpp
空
(3)WhiteFemaleHuman

//WhiteFemaleHuman.h
#ifndef __AbstractFactory__WhiteFemaleHuman__
#define __AbstractFactory__WhiteFemaleHuman__

#include 
#include "WhiteHuman.h"
using std::cout;
using std::endl;

class CWhiteFemaleHuman : public CWhiteHuman
{
public:
    CWhiteFemaleHuman(void)
    {
        
    }
    ~CWhiteFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个女性白种人" << endl;
    }
};

#endif /* defined(__AbstractFactory__WhiteFemaleHuman__) */

//WhiteFemaleHuman.cpp
空
2)Man-Black

(1)BlackHuman

//BlackHuman.h
#ifndef __AbstractFactory__BlackHuman__
#define __AbstractFactory__BlackHuman__
#include 
#include "IHuman.h"

class CBlackHuman : public IHuman
{
public:
    CBlackHuman(void);
    ~CBlackHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__BlackHuman__) */

//BlackHuman.cpp
#include "BlackHuman.h"
#include 
using std::cout;
using std::endl;

CBlackHuman::CBlackHuman(void)
{
    
}
CBlackHuman::~CBlackHuman(void)
{
    
}
void CBlackHuman::Laugh()
{
    cout << "黑种人笑" << endl;
}
void CBlackHuman::Cry()
{
    cout << "黑种人哭" << endl;
}
void CBlackHuman::Talk()
{
    cout << "黑种人说" << endl;
}
(2)BlackMaleHuman
//BlackMaleHuman.h
#include 
#include "BlackHuman.h"
using std::cout;
using std::endl;

class CBlackMaleHuman : public CBlackHuman
{
public:
    CBlackMaleHuman(void)
    {
        
    }
    ~CBlackMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个男性黑种人" << endl;
    }
};

#endif /* defined(__AbstractFactory__BlackMaleHuman__) */

//BlackMaleHuman.cpp
空
(3)BlackFemaleHuman

//BlackFemaleHuman.h
#ifndef __AbstractFactory__BlackFemaleHuman__
#define __AbstractFactory__BlackFemaleHuman__

#include 
#include "BlackHuman.h"
using std::cout;
using std::endl;

class CBlackFemaleHuman : public CBlackHuman
{
public:
    CBlackFemaleHuman(void)
    {
        
    }
    ~CBlackFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个女性黑种人" << endl;
    }
};



#endif /* defined(__AbstractFactory__BlackFemaleHuman__) */

//BlackFemaleHuman.cpp
空
3)Man-Yellow

(1)YellowHuman

//YellowHuman.h
#ifndef __AbstractFactory__YellowHuman__
#define __AbstractFactory__YellowHuman__

#include "IHuman.h"

class CYellowHuman : public IHuman
{
public:
    CYellowHuman(void);
    ~CYellowHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__YellowHuman__) */

//YellowHuman.cpp
#include "YellowHuman.h"
#include 
using std::cout;
using std::endl;

CYellowHuman::CYellowHuman(void)
{
    
}
CYellowHuman::~CYellowHuman(void)
{
    
}
void CYellowHuman::Cry()
{
    cout << "黄种人哭" << endl;
}
void CYellowHuman::Laugh()
{
    cout << "黄种人笑" << endl;
}
void CYellowHuman::Talk()
{
    cout << "黄种人说" << endl;
}
(2)YellowMaleHuman

//YellowMaleHuman.h
#include "YellowHuman.h"
#include 
using std::cout;
using std::endl;

class CYellowMaleHuman : public CYellowHuman
{
public:
    CYellowMaleHuman(void)
    {
        
    }
    ~CYellowMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个男性黄种人" << endl;
    }
};

//YellowMaleHuman.cpp
空
(3)YellowFemaleHuman

//YellowFemaleHuman.h
#ifndef __AbstractFactory__YellowFemaleHuman__
#define __AbstractFactory__YellowFemaleHuman__

#include "YellowHuman.h"
#include 
using std::cout;
using std::endl;

class CYellowFemaleHuman : public CYellowHuman
{
public:
    CYellowFemaleHuman(void)
    {
        
    }
    ~CYellowFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout << "我是一个女性黄种人" << endl;
    }
};


#endif /* defined(__AbstractFactory__YellowFemaleHuman__) */

//YellowFemaleHuman.cpp
空
4)IHuman.h

#ifndef AbstractFactory_IHuman_h
#define AbstractFactory_IHuman_h

class IHuman
{
public:
    IHuman(void)
    {
        
    }
    virtual ~IHuman(void)
    {
        
    }
    virtual void Laugh() = 0;
    virtual void Cry()   = 0;
    virtual void Talk()  = 0;
    virtual void Sex()   = 0;
};

#endif
Factory

(1)IHumanFactory.h

#ifndef AbstractFactory_IHumanFactory_h
#define AbstractFactory_IHumanFactory_h

#include 
#include "IHuman.h"
class IHumanFactory
{
public:
    IHumanFactory(void)
    {
        
    }
    virtual ~IHumanFactory(void)
    {
        
    }
    virtual IHuman* CreateYellowHuman() = 0;
    virtual IHuman* CreateWhiteHuman() = 0;
    virtual IHuman* CreateBlackHuman() = 0;
};
#endif
(2)CStandardHumanFactory

//CStandardHumanFactory.h
#ifndef __AbstractFactory__CStandardHumanFactory__
#define __AbstractFactory__CStandardHumanFactory__

#include 
#include "IHuman.h"
#include "IHumanFactory.h"

template
class CStandardHumanFactory : public IHumanFactory
{
public:
    CStandardHumanFactory(void)
    {
        
    }
    ~CStandardHumanFactory(void)
    {
        
    }
    IHuman* CreateHuman()
    {
        return new T;
    }
};

#endif /* defined(__AbstractFactory__CStandardHumanFactory__) */

//CStandardHumanFactory.cpp
空
(3)MaleHumanFactory.h

//MaleHumanFactory.h

#ifndef __AbstractFactory__MaleHumanFactory__
#define __AbstractFactory__MaleHumanFactory__

#include 
#include "CStandardHumanFactory.h"
#include "IHumanFactory.h"

template
class CMaleHumanFactory : public CStandardHumanFactory
{
public:
    CMaleHumanFactory(void);
    ~CMaleHumanFactory(void);
    IHuman* CreateYellowHuman();
    IHuman* CreateWhiteHuman();
    IHuman* CreateBlackHuman();
};

#endif /* defined(__AbstractFactory__MaleHumanFactory__) */

//MaleHumanFactory.cpp
#include "MaleHumanFactory.h"

template
CMaleHumanFactory::CMaleHumanFactory(void)
{
    
}
template
CMaleHumanFactory::~CMaleHumanFactory(void)
{
    
}
template
IHuman* CMaleHumanFactory::CreateYellowHuman()
{
    return CStandardHumanFactory::CreateHuman();
}
template
IHuman* CMaleHumanFactory::CreateWhiteHuman()
{
    return CStandardHumanFactory::CreateHuman();
}
template
IHuman* CMaleHumanFactory::CreateBlackHuman()
{
    return CStandardHumanFactory::CreateHuman();
}
(4)FemaleHumanFactory

//FemaleHumanFactory.h
#ifndef __AbstractFactory__FemaleHumanFactory__
#define __AbstractFactory__FemaleHumanFactory__

#include 
#include "CStandardHumanFactory.h"
#include "IHuman.h"

template
class CFemaleHumanFactory : public CStandardHumanFactory
{
public:
    CFemaleHumanFactory(void)
    {
        
    }
    ~CFemaleHumanFactory(void)
    {
        
    }
    IHuman* CreateYellowHuman()
    {
        return CStandardHumanFactory::CreateHuman();
    }
    IHuman* CreateWhiteHuman()
    {
        return CStandardHumanFactory::CreateHuman();
    }
    IHuman* CreateBlackHuman()
    {
        return CStandardHumanFactory::CreateHuman();
    }
    
};

#endif /* defined(__AbstractFactory__FemaleHumanFactory__) */

//FemaleHumanFactory.cpp
空
Main.cpp

#include 
#include 
#include "IHuman.h"
#include "IHumanFactory.h"
#include "FemaleHumanFactory.h"
#include "MaleHumanFactory.h"
#include "MaleHumanFactory.cpp"
#include "YellowFemaleHuman.h"
#include "YellowMaleHuman.h"
#include "WhiteFemaleHuman.h"
#include "WhiteMaleHuman.h"
#include "BlackFemaleHuman.h"
#include "BlackMaleHuman.h"

void DoMakeHuman()
{
    cout << "----------开始创建黄种人----------" << endl;
    cout << "*****开始创建黄种女人*****" << endl;
    IHumanFactory* pFemaleHumanFactory = new CFemaleHumanFactory();
    IHuman* pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
    pYellowFemaleHuman->Cry();
    pYellowFemaleHuman->Laugh();
    pYellowFemaleHuman->Talk();
    pYellowFemaleHuman->Sex();
    delete pYellowFemaleHuman;
    delete pFemaleHumanFactory;
    cout << "*****开始创建黄种男人*****" << endl;
    IHumanFactory* pMaleHumanFactory = new CMaleHumanFactory();
    IHuman* pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
    pYellowMaleHuman->Cry();
    pYellowMaleHuman->Laugh();
    pYellowMaleHuman->Talk();
    pYellowMaleHuman->Sex();
    delete pYellowMaleHuman;
    delete pMaleHumanFactory;
    
    cout << "----------开始创建白种人----------" << endl;
    cout << "*****开始创建白种男人*****" << endl;
    IHumanFactory* pMaleHumanFactory1 = new CMaleHumanFactory();
    IHuman* pWhiteMaleHuman = pMaleHumanFactory1->CreateWhiteHuman();
    pWhiteMaleHuman->Cry();
    pWhiteMaleHuman->Laugh();
    pWhiteMaleHuman->Talk();
    pWhiteMaleHuman->Sex();
    delete pMaleHumanFactory1;
    delete pWhiteMaleHuman;
    cout << "*****开始创建白种女人*****" << endl;
    IHumanFactory* pFemaleHumanFactory1 = new CFemaleHumanFactory();
    IHuman* pWhiteFemaleHuman = pFemaleHumanFactory1->CreateWhiteHuman();
    pWhiteFemaleHuman->Cry();
    pWhiteFemaleHuman->Laugh();
    pWhiteFemaleHuman->Talk();
    pWhiteFemaleHuman->Sex();
    delete pFemaleHumanFactory1;
    delete pWhiteFemaleHuman;
    cout << "----------开始创建黑种人----------" << endl;
    cout << "*****开始创建黑种男人*****" << endl;
    IHumanFactory* pMaleHumanFactory2 = new CMaleHumanFactory();
    IHuman* pBlackMaleHuman = pMaleHumanFactory2->CreateBlackHuman();
    pBlackMaleHuman->Cry();
    pBlackMaleHuman->Laugh();
    pBlackMaleHuman->Talk();
    pBlackMaleHuman->Sex();
    delete pMaleHumanFactory2;
    delete pBlackMaleHuman;
    cout << "*****开始创建黑种女人*****" << endl;
    IHumanFactory* pFemaleHumanFactory2 = new CFemaleHumanFactory();
    IHuman* pBlackFemaleHuman = pFemaleHumanFactory2->CreateBlackHuman();
    pBlackFemaleHuman->Cry();
    pBlackFemaleHuman->Laugh();
    pBlackFemaleHuman->Talk();
    pBlackFemaleHuman->Sex();
    delete pFemaleHumanFactory2;
    delete pBlackFemaleHuman;
}

int main(int argc, const char * argv[]) {

    DoMakeHuman();
    std::cout << "Hello, World!\n";
    return 0;
}

4、运行截图




参考资料:http://blog.csdn.net/rexuefengye/article/details/12704821,感谢my183100521博主的精彩博文。

人气教程排行