首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计方式(c++)笔记之二(AbstractFactory抽象工厂模式)

2013-10-15 
设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)一、简介:抽象工厂,提供一个创建一系列相关或相互依赖

设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)
一、简介:

  

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


注释:

    main(),女娲

    IHuman,产品接口

    CYellowHuman,抽象产品之一

    CYellowFemaleHuman,具体产品之一

    CYellowMaleHuman,具体产品之二

    CWhiteHuman,抽象产品之二

    CWhiteFemaleHuman,具体产品之三

    CWhiteMaleHuman,具体产品之四

    CBlackHuman,抽象产品之三

    CBlackFemaleHuman,具体产品之五

    CBlackMaleHuman,具体产品之六

    IHumanFactory,抽象工厂

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

    CFemaleHumanFactory,工厂之一

    CMaleHumanFactory,工厂之二


我的工程目录:


设计方式(c++)笔记之二(AbstractFactory抽象工厂模式)


三、实现代码

产品接口类IHuman

IHuman.h

#include "BlackHuman.h"#include <iostream>using std::cout;using std::endl;CBlackHuman::CBlackHuman(void){}CBlackHuman::~CBlackHuman(void){}void CBlackHuman::Cry(){    cout << "黑人会哭" << endl;}void CBlackHuman::Laugh(){    cout << "黑人会笑" << endl;}void CBlackHuman::Talk(){    cout << "黑人可以说话,一般人听不懂" << endl;}

黑种人(男)具体实现类BlackMaleHuman

BlackMaleHuman.h

#ifndef __AbstractFactory__BlackFemaleHuman__#define __AbstractFactory__BlackFemaleHuman__#include <iostream>#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__) */

白种人抽象类WhiteHuman

WhiteHuman.h

#include "WhiteHuman.h"#include <iostream>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;}

白种人(男)具体实现类WhiteMaleHuman

WhiteMaleHuman.h

#ifndef __AbstractFactory__WhiteMaleHuman__#define __AbstractFactory__WhiteMaleHuman__#include <iostream>#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__) */

白种人(男)具体实现类WhiteFemaleHuman

WhiteFemaleHuman.h

#include "YellowHuman.h"#include <iostream>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;}

黄种人(男)具体实现类YellowMaleHuman

YellowMaleHuman.h

#ifndef __AbstractFactory__YellowMaleHuman__#define __AbstractFactory__YellowMaleHuman__#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 /* defined(__AbstractFactory__YellowMaleHuman__) */

黄种人(女)具体实现类YellowFemaleHuman

YellowFemaleHuman.h

#ifndef __AbstractFactory__StandardHumanFactory__#define __AbstractFactory__StandardHumanFactory__#include <iostream>#include "IHumanFactory.h"#include "IHuman.h"template<class T>class CStandardHumanFactory:public IHumanFactory{public:    CStandardHumanFactory(void)    {    }    ~CStandardHumanFactory(void)    {    }    IHuman * CreateHuman()    {        return new T;    }};#endif /* defined(__AbstractFactory__StandardHumanFactory__) */

创造(男)工厂类MaleHumanFactory

MaleHumanFactory.h

#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();    return CStandardHumanFactory<T>::CreateHuman();}template<class T>IHuman * CMaleHumanFactory<T>::CreateWhiteHuman(){    //return CreateHuman();    return CStandardHumanFactory<T>::CreateHuman();}template<class T>IHuman * CMaleHumanFactory<T>::CreateBlackHuman(){   // return CreateHuman();    return CStandardHumanFactory<T>::CreateHuman();}

创造(女)工厂类MaleHumanFactory

#ifndef __AbstractFactory__FemaleHumanFactory__#define __AbstractFactory__FemaleHumanFactory__#include <iostream>#include "standardhumanfactory.h"#include "IHuman.h"template<class T>class CFemaleHumanFactory:public CStandardHumanFactory<T> {        public:     CFemaleHumanFactory(void)     {     }     ~CFemaleHumanFactory(void)     {     }     IHuman * CreateYellowHuman()     {         return CStandardHumanFactory<T>::CreateHuman();        //return CreateHuman();     }     IHuman * CreateWhiteHuman()     {         return CStandardHumanFactory<T>::CreateHuman();        // return CreateHuman();     }     IHuman * CreateBlackHuman()     {         return CStandardHumanFactory<T>::CreateHuman();         //return CreateHuman();     }};#endif /* defined(__AbstractFactory__FemaleHumanFactory__) */

主类main.cpp



参考文献:《设计模式之禅》,《GoF_23种设计模式解析》

参考博客:http://www.cnblogs.com/wanggary/archive/2011/04/13/2015288.html


热点排行