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

一、简介:

  

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

AbstractFactory 模式典型的结构图为: 


设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)_第1张图片

图 2-1:AbstractFactory Pattern 结构图


AbstractFactory 模式关键就是将这一组对象的创建封装到一个用于创建对象的类

(ConcreteFactory)中,维护这样一个创建类总比维护n多相关对象的创建过程要简单的多。 


二、实例


    上一篇博文讲到女娲造人,人是造出来了,世界时热闹了,可是低头一看,都是清一色的类型,缺少关爱、仇恨、喜怒哀乐等情绪,人类的生命太平淡了,女娲一想,猛然一拍脑袋,Shit!忘记给人类定义性别了,那怎么办?抹掉重来,然后就把人类重新洗牌,准备重新开始制造人类。

由于先前的工作已经花费了很大的精力做为铺垫,也不想从头开始了,那先说人类(Product 产品类)怎么改吧,好,有了,给每个人类都加一个性别,然后再重新制造,这个问题解决了,那八卦炉怎么办?只有一个呀,要么生产出全都是男性,要不都是女性,那不行呀,有了,把已经有了一条生产线——八卦炉(工厂模式中的 Concrete Factory)拆开,于是女娲就使用了“八卦拷贝术”,把原先的八卦炉一个变两个,并且略加修改,就成了女性八卦炉(只生产女性,一个具体工厂的实现类)和男性八卦炉(只生产男性,又一个具体工厂的实现类),这个过程的类图如下: 


设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)_第2张图片


注释:

    main(),女娲

    IHuman,产品接口

    CYellowHuman,抽象产品之一

    CYellowFemaleHuman,具体产品之一

    CYellowMaleHuman,具体产品之二

    CWhiteHuman,抽象产品之二

    CWhiteFemaleHuman,具体产品之三

    CWhiteMaleHuman,具体产品之四

    CBlackHuman,抽象产品之三

    CBlackFemaleHuman,具体产品之五

    CBlackMaleHuman,具体产品之六

    IHumanFactory,抽象工厂

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

    CFemaleHumanFactory,工厂之一

    CMaleHumanFactory,工厂之二


我的工程目录:


      设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)_第3张图片


三、实现代码

产品接口类IHuman

IHuman.h

#ifndef __Factory__IHuman__
#define __Factory__IHuman__



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 /* defined(__Factory__IHuman__) */

黑种人抽象类BlackHuman

BlackHuman.h

#ifndef __Factory__BlackHuman__
#define __Factory__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(__Factory__BlackHuman__) */
BlackHuman.cpp

#include "BlackHuman.h"
#include 
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__BlackMaleHuman__
#define __AbstractFactory__BlackMaleHuman__

#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__) */

黑种人(女)具体实现类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__) */

白种人抽象类WhiteHuman

WhiteHuman.h

#ifndef __Factory__WhiteHuman__
#define __Factory__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(__Factory__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;
}

白种人(男)具体实现类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__) */

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

WhiteFemaleHuman.h

#ifndef __AbstractFactory__WhiteFemaleHuman__
#define __AbstractFactory__WhiteFemaleHuman__

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

class CWhiteFemaleHuman : public CWhiteHuman
{
public:
    CWhiteFemaleHuman(void)
    {
    }
    ~CWhiteFemaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该白种人的性别为女..." << endl;
    }
};

#endif /* defined(__AbstractFactory__WhiteFemaleHuman__) */

黄种人抽象类YellowHuman

YellowHuman.h

#ifndef __Factory__YellowHuman__
#define __Factory__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(__Factory__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;
}

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

YellowMaleHuman.h

#ifndef __AbstractFactory__YellowMaleHuman__
#define __AbstractFactory__YellowMaleHuman__

#include "yellowhuman.h"
#include 
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__YellowFemaleHuman__
#define __AbstractFactory__YellowFemaleHuman__

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

class CYellowFemaleHuman :
public CYellowHuman
{
public:
    CYellowFemaleHuman(void)
    {
    }
    ~CYellowFemaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该黄种人的性别为女..." << endl;
    }
};

#endif /* defined(__AbstractFactory__YellowFemaleHuman__) */

抽象工厂类IHumanFactory

IHumanFactory.h

#ifndef __AbstractFactory__IHumanFactory__
#define __AbstractFactory__IHumanFactory__

#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 /* defined(__AbstractFactory__IHumanFactory__) */

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

StandardHumanFactory.h

#ifndef __AbstractFactory__StandardHumanFactory__
#define __AbstractFactory__StandardHumanFactory__

#include 

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

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

#endif /* defined(__AbstractFactory__StandardHumanFactory__) */

创造(男)工厂类MaleHumanFactory

MaleHumanFactory.h

#ifndef __AbstractFactory__MaleHumanFactory__
#define __AbstractFactory__MaleHumanFactory__

#include 
#include "StandardHumanFactory.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 CreateHuman();
    return CStandardHumanFactory::CreateHuman();
}

template
IHuman * CMaleHumanFactory::CreateWhiteHuman()
{
    //return CreateHuman();
    return CStandardHumanFactory::CreateHuman();
}

template
IHuman * CMaleHumanFactory::CreateBlackHuman()
{
   // return CreateHuman();
    return CStandardHumanFactory::CreateHuman();
}

创造(女)工厂类MaleHumanFactory

#ifndef __AbstractFactory__FemaleHumanFactory__
#define __AbstractFactory__FemaleHumanFactory__

#include 
#include "standardhumanfactory.h"
#include "IHuman.h"
template
class CFemaleHumanFactory:public CStandardHumanFactory
 {
    
    
public:
     CFemaleHumanFactory(void)
     {
     }
     ~CFemaleHumanFactory(void)
     {
     }
     IHuman * CreateYellowHuman()
     {
         return CStandardHumanFactory::CreateHuman();
        //return CreateHuman();
     }
     IHuman * CreateWhiteHuman()
     {
         return CStandardHumanFactory::CreateHuman();
        // return CreateHuman();
     }
     IHuman * CreateBlackHuman()
     {
         return CStandardHumanFactory::CreateHuman();
         //return CreateHuman();
     }
};

#endif /* defined(__AbstractFactory__FemaleHumanFactory__) */

主类main.cpp

#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 DoIt()
{
    IHumanFactory *pFemaleHumanFactory = new CFemaleHumanFactory();
    IHuman *pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
    pYellowFemaleHuman->Cry();
    pYellowFemaleHuman->Laugh();
    pYellowFemaleHuman->Talk();
    pYellowFemaleHuman->Sex();
    delete pYellowFemaleHuman;
    delete pFemaleHumanFactory;
    
    IHumanFactory *pMaleHumanFactory = new CMaleHumanFactory();
    IHuman *pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
    pYellowMaleHuman->Cry();
    pYellowMaleHuman->Laugh();
    pYellowMaleHuman->Talk();
    pYellowMaleHuman->Sex();
    delete pYellowMaleHuman;
    delete pMaleHumanFactory;
}


int main(int argc, const char * argv[])
{
    DoIt();
 
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

结果如下:


设计模式(c++)笔记之二(AbstractFactory抽象工厂模式)_第4张图片


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

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


你可能感兴趣的:(设计模式(C++),设计模式(c++)笔记)