欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2023-12-30 11:49:34
...

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

注释:

main(),女娲

IHuman,产品接口

CYellowHuman,抽象产品之一

CYellowFemaleHuman,具体产品之一

CYellowMaleHuman,具体产品之二

CWhiteHuman,抽象产品之二

CWhiteFemaleHuman,具体产品之三

CWhiteMaleHuman,具体产品之四

CBlackHuman,抽象产品之三

CBlackFemaleHuman,具体产品之五

CBlackMaleHuman,具体产品之六

IHumanFactory,抽象工厂

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

CFemaleHumanFactory,工厂之一

CMaleHumanFactory,工厂之二

工程目录:

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

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 (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 (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 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 (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 (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 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 (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 (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 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 ();
    IHuman* pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
    pYellowFemaleHuman->Cry();
    pYellowFemaleHuman->Laugh();
    pYellowFemaleHuman->Talk();
    pYellowFemaleHuman->Sex();
    delete pYellowFemaleHuman;
    delete pFemaleHumanFactory;
    cout ();
    IHuman* pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
    pYellowMaleHuman->Cry();
    pYellowMaleHuman->Laugh();
    pYellowMaleHuman->Talk();
    pYellowMaleHuman->Sex();
    delete pYellowMaleHuman;
    delete pMaleHumanFactory;
    
    cout ();
    IHuman* pWhiteMaleHuman = pMaleHumanFactory1->CreateWhiteHuman();
    pWhiteMaleHuman->Cry();
    pWhiteMaleHuman->Laugh();
    pWhiteMaleHuman->Talk();
    pWhiteMaleHuman->Sex();
    delete pMaleHumanFactory1;
    delete pWhiteMaleHuman;
    cout ();
    IHuman* pWhiteFemaleHuman = pFemaleHumanFactory1->CreateWhiteHuman();
    pWhiteFemaleHuman->Cry();
    pWhiteFemaleHuman->Laugh();
    pWhiteFemaleHuman->Talk();
    pWhiteFemaleHuman->Sex();
    delete pFemaleHumanFactory1;
    delete pWhiteFemaleHuman;
    cout ();
    IHuman* pBlackMaleHuman = pMaleHumanFactory2->CreateBlackHuman();
    pBlackMaleHuman->Cry();
    pBlackMaleHuman->Laugh();
    pBlackMaleHuman->Talk();
    pBlackMaleHuman->Sex();
    delete pMaleHumanFactory2;
    delete pBlackMaleHuman;
    cout ();
    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 

4、运行截图

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



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

上一篇:

下一篇: