c++设计模式五,抽象工厂模式

来源:互联网 发布:刺客信条大革命优化版 编辑:程序博客网 时间:2024/05/06 23:17

抽象工厂,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。对于工厂方法来说,抽象工厂可实现一系列产品的生产,抽象工厂更注重产品的组合。

blackhuman.h

#ifndef BLACKHUMAN_H#define BLACKHUMAN_H#include "ihuman.h"#include <iostream>using std::cout;using std::endl;class CBlackHuman :    public IHuman{public:    CBlackHuman(void)    {    }    ~CBlackHuman(void)    {    }    void Laugh()    {        cout << "黑人会笑" << endl;    }    void Cry()    {        cout << "黑人会哭" << endl;    }    void Talk()    {        cout << "黑人可以说话,一般人听不懂" << endl;    }    virtual void Sex() = 0;};#endif // BLACKHUMAN_H
blackfemalehuman.h
#ifndef BLACKFEMALEHUMAN_H#define BLACKFEMALEHUMAN_H#include "blackhuman.h"#include <iostream>using std::cout;using std::endl;class CBlackFemaleHuman :    public CBlackHuman{public:    CBlackFemaleHuman(void)    {    }    ~CBlackFemaleHuman(void)    {    }    void Sex()    {        cout << "该黑种人的性别为女..." << endl;    }};#endif // BLACKFEMALEHUMAN_H
blackmalehuman.h
#ifndef BLACKMALEHUMAN_H#define BLACKMALEHUMAN_H#include "blackhuman.h"#include <iostream>using std::cout;using std::endl;class CBlackMaleHuman :    public CBlackHuman{public:    CBlackMaleHuman(void)    {    }    ~CBlackMaleHuman(void)    {    }    void Sex()    {        cout << "该黑种人的性别为男..." << endl;    }};#endif // BLACKMALEHUMAN_H
femalehumanfactory.h
#ifndef FEMALEHUMANFACTORY_H#define FEMALEHUMANFACTORY_H#include "standardhumanfactory.h"template<class T>class CFemaleHumanFactory :    public CStandardHumanFactory<T>{public:    CFemaleHumanFactory(void)    {    }    ~CFemaleHumanFactory(void)    {    }    IHuman * CreateYellowHuman()    {        return CreateHuman();    }    IHuman * CreateWhiteHuman()    {        return CreateHuman();    }    IHuman * CreateBlackHuman()    {        return CreateHuman();    }};#endif // FEMALEHUMANFACTORY_H
ihuman.h
#ifndef IHUMAN_H#define IHUMAN_Hclass IHuman{public:    IHuman(void)    {    }    virtual ~IHuman(void)    {    }    virtual void Laugh() = 0;    virtual void Cry() = 0;    virtual void Talk() = 0;    virtual void Sex() = 0;};#endif // IHUMAN_H
ihumanfactory.h
#ifndef IHUMANFACTORY_H#define IHUMANFACTORY_H#include "IHuman.h"class IHumanFactory{public:    IHumanFactory(void)    {    }    virtual ~IHumanFactory(void)    {    }    virtual IHuman * CreateYellowHuman() = 0;    virtual IHuman * CreateWhiteHuman() = 0;    virtual IHuman * CreateBlackHuman() = 0;};#endif // IHUMANFACTORY_H
standardhumanfactory.h
#ifndef STANDARDHUMANFACTORY_H#define STANDARDHUMANFACTORY_H#include "ihumanfactory.h"#include "IHuman.h"template<class T>class CStandardHumanFactory :    public IHumanFactory{public:    CStandardHumanFactory(void)    {    }    ~CStandardHumanFactory(void)    {    }    IHuman * CreateHuman()    {        return new T;    }};#endif // STANDARDHUMANFACTORY_H
whitefemalehuman.h
#ifndef WHITEFEMALEHUMAN_H#define WHITEFEMALEHUMAN_H#include "whitehuman.h"#include <iostream>using std::cout;using std::endl;class CWhiteFemaleHuman :    public CWhiteHuman{public:    CWhiteFemaleHuman(void)    {    }    ~CWhiteFemaleHuman(void)    {    }    void Sex()    {        cout << "该白种人的性别为女..." << endl;    }};#endif // WHITEFEMALEHUMAN_H
whitehuman.h
#ifndef WHITEHUMAN_H#define WHITEHUMAN_H#include "ihuman.h"#include <iostream>using std::cout;using std::endl;class CWhiteHuman :    public IHuman{public:    CWhiteHuman(void)    {    }    ~CWhiteHuman(void)    {    }    void Laugh()    {        cout << "白色人种会大笑,侵略的笑声" << endl;    }    void Cry()    {        cout << "白色人种会哭" << endl;    }    void Talk()    {        cout << "白色人种会说话,一般都是单字节" << endl;    }    virtual void Sex() = 0;};#endif // WHITEHUMAN_H
whitemalehuman.h
#ifndef WHITEMALEHUMAN_H#define WHITEMALEHUMAN_H#include "whitehuman.h"#include <iostream>using std::cout;using std::endl;class CWhiteMaleHuman :    public CWhiteHuman{public:    CWhiteMaleHuman(void)    {    }    ~CWhiteMaleHuman(void)    {    }    void Sex()    {        cout << "该白种人的性别为男..." << endl;    }};#endif // WHITEMALEHUMAN_H
yellowfemalehuman.h

#ifndef YELLOWFEMALEHUMAN_H#define YELLOWFEMALEHUMAN_H#include "yellowhuman.h"#include <iostream>using std::cout;using std::endl;class CYellowFemaleHuman :    public CYellowHuman{public:    CYellowFemaleHuman(void)    {    }    ~CYellowFemaleHuman(void)    {    }    void Sex()    {        cout << "该黄种人的性别为女..." << endl;    }};#endif // YELLOWFEMALEHUMAN_H


yellowhuman.h

#ifndef YELLOWHUMAN_H#define YELLOWHUMAN_H#include "ihuman.h"#include <iostream>using std::cout;using std::endl;class CYellowHuman :    public IHuman{public:    CYellowHuman(void)    {    }    ~CYellowHuman(void)    {    }    void Laugh()    {        cout << "黄色人种会大笑,幸福呀!" << endl;    }    void Cry()    {        cout << "黄色人种会哭" << endl;    }    void Talk()    {        cout << "黄色人种会说话,一般说的都是双字节" << endl;    }    virtual void Sex() = 0;};#endif // YELLOWHUMAN_H
yellowmalehuman.h
#ifndef YELLOWMALEHUMAN_H#define YELLOWMALEHUMAN_H#include "yellowhuman.h"#include <iostream>using std::cout;using std::endl;class CYellowMaleHuman :    public CYellowHuman{public:    CYellowMaleHuman(void)    {    }    ~CYellowMaleHuman(void)    {    }    void Sex()    {        cout << "该黄种人的性别为男..." << endl;    }};#endif // YELLOWMALEHUMAN_H
malehumanfactory.h
#ifndef MALEHUMANFACTORY_H#define MALEHUMANFACTORY_H#include "standardhumanfactory.h"#include "IHumanFactory.h"template<class T>class CMaleHumanFactory :    public CStandardHumanFactory<T>{public:    CMaleHumanFactory(void);    ~CMaleHumanFactory(void);    IHuman * CreateYellowHuman();    IHuman * CreateWhiteHuman();    IHuman * CreateBlackHuman();};#endif // MALEHUMANFACTORY_H
malehumanfactory.cpp
#include "MaleHumanFactory.h"template<class T>CMaleHumanFactory<T>::CMaleHumanFactory(void){}template<class T>CMaleHumanFactory<T>::~CMaleHumanFactory(void){}template<class T>IHuman * CMaleHumanFactory<T>::CreateYellowHuman(){    return CreateHuman();}template<class T>IHuman * CMaleHumanFactory<T>::CreateWhiteHuman(){    return CreateHuman();}template<class T>IHuman * CMaleHumanFactory<T>::CreateBlackHuman(){    return CreateHuman();}
main.cpp

#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 DoIt()
{
    IHumanFactory *pFemaleHumanFactory = new CFemaleHumanFactory<CYellowFemaleHuman>();
    IHuman *pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
    pYellowFemaleHuman->Cry();
    pYellowFemaleHuman->Laugh();
    pYellowFemaleHuman->Talk();
    pYellowFemaleHuman->Sex();
    delete pYellowFemaleHuman;
    delete pFemaleHumanFactory;
    IHumanFactory *pMaleHumanFactory = new CMaleHumanFactory<CYellowMaleHuman>();
    IHuman *pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
    pYellowMaleHuman->Cry();
    pYellowMaleHuman->Laugh();
    pYellowMaleHuman->Talk();
    pYellowMaleHuman->Sex();
    delete pYellowMaleHuman;
    delete pMaleHumanFactory;
}
int main(int argc, char* argv[])
{
    DoIt();
    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
    _CrtDumpMemoryLeaks();
    return 0;
}
运行结果:




原创粉丝点击