设计模式(c++)笔记之一(Factory Method工厂方法模式)

来源:互联网 发布:虚拟刻录机软件 编辑:程序博客网 时间:2024/06/04 19:09

    工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。


一、模式选择

我们通常使用 Factory 模式来解决上面给出的两个问题。在第一个问题中,我们经常就是声明一个创建对象的接口,并封装了对象的创建过程。Factory 这里类似于一个真正意义上的工厂(生产对象)。在第二个问题中,我们需要提供一个对象创建对象的接口,并在子类中提供其具体实现(因为只有在子类中可以决定到底实例化哪一个类)。 

第一中情况的 Factory 的结构示意图为:


1:Factory模式结构示意图1


图 1 所以的 Factory 模式经常在系统开发中用到,但是这并不是 Factory 模式的最大威

力所在(因为这可以通过其他方式解决这个问题)。Factory 模式不单是提供了创建对象的接口,其最重要的是延迟了子类的实例化(第二个问题),以下是这种情况的一个 Factory 的结构示意图: 


2:Factory模式结构示意图1


图 2 中关键中 Factory 模式的应用并不是只是为了封装对象的创建,而是要把对象的创建放到子类中实现:Factory 中只是提供了对象创建的接口,其实现将放在 Factory 的子类ConcreteFactory 中进行。这是图 2 和图 1 的区别所在。 

二、实例:


1.描述


    女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了。这个故事是说,女娲在补了天后,下到凡间一看,哇塞,风景太优美了,天空是湛蓝的,水是清澈的,空气是清新的,太美丽了,然后就待时间长了就有点寂寞了,没有动物,这些看的到都是静态的东西呀,怎么办?

别忘了是神仙呀,没有办不到的事情,于是女娲就架起了八卦炉(技术术语:建立工厂)开始创建人,具体过程是这样的:先是泥巴捏,然后放八卦炉里烤,再扔到地上成长,但是意外总是会产生的:第一次烤泥人,兹兹兹兹~~,感觉应该熟了,往地上一扔,biu~,一个白人诞生了,没烤熟!

第二次烤泥人,兹兹兹兹兹兹兹兹~~,上次都没烤熟,这次多烤会儿,往地上一扔,嘿,熟过头了,黑人哪!

第三次烤泥人,兹~兹~兹~,一边烤一边看着,嘿,正正好,Perfect!优品,黄色人类!

这个过程还是比较有意思的,先看看类图: 




2.我的工程目录:


      



注释:

    main(),女娲

    IHuman,产品接口

    CYellowHuman,产品之一

    CWhiteHuman,产品之二

    CBlackHuman,产品之三

    IHumanFactory,工厂接口

    CYellowHumanFactory,工厂之一

    CWhiteHumanFactory,工厂之二

    CBlackHumanFactory,工厂之三


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;};#endif /* defined(__Factory__IHuman__) */

黄种人类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();};#endif /* defined(__Factory__YellowHuman__) */
YellowHuman.cpp

#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;}

白种人类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();};#endif /* defined(__Factory__WhiteHuman__) */
WhiteHuman.cpp

#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;}

黑种人BlackHuman

BlackHuman.h

#ifndef __Factory__BlackHuman__#define __Factory__BlackHuman__#include <iostream>#include "IHuman.h"class CBlackHuman :public IHuman{public:    CBlackHuman(void);    ~CBlackHuman(void);    void Laugh();    void Cry();    void Talk();};#endif /* defined(__Factory__BlackHuman__) */
BlackHuman.cpp

#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;}

工厂接口IHumanFactory

IHumanFactory.h

#ifndef __Factory__IHumanFactory__#define __Factory__IHumanFactory__#include <iostream>#include "IHuman.h"class IHumanFactory{public:    IHumanFactory(void)    {    }    virtual ~IHumanFactory(void)    {    }    virtual IHuman * CreateHuman() = 0;};#endif /* defined(__Factory__IHumanFactory__) */

创造黄种人工厂类YellowHumanFactory

YellowHumanFactory.h

#ifndef __Factory__YellowHumanFactory__#define __Factory__YellowHumanFactory__#include <iostream>#include "IHumanFactory.h"class CYellowHumanFactory:public IHumanFactory{public:    CYellowHumanFactory(void);    ~CYellowHumanFactory(void);    virtual IHuman * CreateHuman(void);};#endif /* defined(__Factory__YellowHumanFactory__) */
YellowHumanFactory.cpp

#include "YellowHumanFactory.h"#include "YellowHuman.h"CYellowHumanFactory::CYellowHumanFactory(void){    }CYellowHumanFactory::~CYellowHumanFactory(void){    }IHuman *CYellowHumanFactory::CreateHuman(void){    return new CYellowHuman();    }

创造白种人工厂类WhiteHumanFactory.h

WhiteHumanFactory.h

#ifndef __Factory__WhiteHumanFactory__#define __Factory__WhiteHumanFactory__#include <iostream>#include "IHumanFactory.h"class CWhiteHumanFactory :public IHumanFactory{public:    CWhiteHumanFactory(void);    ~CWhiteHumanFactory(void);    virtual IHuman * CreateHuman(void);};#endif /* defined(__Factory__WhiteHumanFactory__) */
WhiteHumanFactory.cpp

#include "WhiteHumanFactory.h"#include "WhiteHuman.h"CWhiteHumanFactory::CWhiteHumanFactory(void){}CWhiteHumanFactory::~CWhiteHumanFactory(void){}IHuman * CWhiteHumanFactory::CreateHuman( void ){    return new CWhiteHuman();}

创造黑种人工厂类BlackHumanFactory

BlackHumanFactory.h

#ifndef __Factory__BlackHumanFactory__#define __Factory__BlackHumanFactory__#include <iostream>#include "IHumanFactory.h"class CBlackHumanFactory :public IHumanFactory{public:    CBlackHumanFactory(void);    ~CBlackHumanFactory(void);    virtual IHuman * CreateHuman();};#endif /* defined(__Factory__BlackHumanFactory__) */
BlackHumanFactory.cpp

#include "BlackHumanFactory.h"#include "BlackHuman.h"CBlackHumanFactory::CBlackHumanFactory(void){}CBlackHumanFactory::~CBlackHumanFactory(void){}IHuman * CBlackHumanFactory::CreateHuman(){    return new CBlackHuman();}

主类main.cpp

#include <stdio.h>#include "IHuman.h"#include "YellowHuman.h"#include "WhiteHuman.h"#include "BlackHuman.h"#include "IHumanFactory.h"#include "YellowHumanFactory.h"#include "WhiteHumanFactory.h"#include "BlackHumanFactory.h"#include <iostream>using std::cout;using std::endl;using std::string;void DoFactoryMethod1(){    cout << "----------第一批人是这样的:黄种人工厂来生产黄种人" << endl;    IHumanFactory *pHumanFactory = new CYellowHumanFactory();    IHuman *pHuman = pHumanFactory->CreateHuman();    pHuman->Cry();    pHuman->Laugh();    pHuman->Talk();    delete pHuman;    delete pHumanFactory;}void DoFactoryMethod2(){    cout << "----------第二批人是这样的:白种人工厂来生产白种人" << endl;    IHumanFactory *pHumanFactory = new CWhiteHumanFactory();    IHuman *pHuman = pHumanFactory->CreateHuman();    pHuman->Cry();    pHuman->Laugh();    pHuman->Talk();    delete pHuman;    delete pHumanFactory;}void DoFactoryMethod3(){    cout << "----------第一批人是这样的:黑种人工厂来生产黑种人" << endl;    IHumanFactory *pHumanFactory = new CBlackHumanFactory();    IHuman *pHuman = pHumanFactory->CreateHuman();    pHuman->Cry();    pHuman->Laugh();    pHuman->Talk();    delete pHuman;    delete pHumanFactory;}int main(int argc, const char * argv[]){        DoFactoryMethod1();    DoFactoryMethod2();    DoFactoryMethod3();    // insert code here...    printf("Hello, World!\n");    return 0;}

结果如下:


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

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

原创粉丝点击