注释: main(),女娲 ihuman,产品接口 cyellowhuman,抽象产品之一 cyellowfemalehuman,具体产品之一 cyellowmalehuman,具体产品之二 cwhitehuman,抽象产品之二 cwhitefemalehuman,具体产品之三 cwhitemalehuman,具体产品之四 cblackhuman,抽象产品之
注释:
main(),女娲
ihuman,产品接口
cyellowhuman,抽象产品之一
cyellowfemalehuman,具体产品之一
cyellowmalehuman,具体产品之二
cwhitehuman,抽象产品之二
cwhitefemalehuman,具体产品之三
cwhitemalehuman,具体产品之四
cblackhuman,抽象产品之三
cblackfemalehuman,具体产品之五
cblackmalehuman,具体产品之六
ihumanfactory,抽象工厂
cstandardhumanfactory,抽象工厂基类(此类可有可无)
cfemalehumanfactory,工厂之一
cmalehumanfactory,工厂之二
工程目录:
3、代码分析1)man-white
(1)whitehuman
//whitehuman.h#ifndef __abstractfactory__whitehuman__#define __abstractfactory__whitehuman__#include ihuman.hclass 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.husing 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.husing 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.hclass 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.husing 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.husing 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.hclass 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_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
factory(1)ihumanfactory.h
#ifndef abstractfactory_ihumanfactory_h#define abstractfactory_ihumanfactory_h#include #include ihuman.hclass 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.htemplateclass 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.htemplateclass cmalehumanfactory : public cstandardhumanfactory{public: cmalehumanfactory(void); ~cmalehumanfactory(void); ihuman* createyellowhuman(); ihuman* createwhitehuman(); ihuman* createblackhuman();};#endif /* defined(__abstractfactory__malehumanfactory__) *///malehumanfactory.cpp#include malehumanfactory.htemplatecmalehumanfactory::cmalehumanfactory(void){ }templatecmalehumanfactory::~cmalehumanfactory(void){ }templateihuman* cmalehumanfactory::createyellowhuman(){ return cstandardhumanfactory::createhuman();}templateihuman* cmalehumanfactory::createwhitehuman(){ return cstandardhumanfactory::createhuman();}templateihuman* cmalehumanfactory::createblackhuman(){ return cstandardhumanfactory::createhuman();}
(4)femalehumanfactory
//femalehumanfactory.h#ifndef __abstractfactory__femalehumanfactory__#define __abstractfactory__femalehumanfactory__#include #include cstandardhumanfactory.h#include ihuman.htemplateclass 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.hvoid 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、运行截图
参考资料:http://blog.csdn.net/rexuefengye/article/details/12704821,感谢my183100521博主的精彩博文。