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_Hblackfemalehuman.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_Hblackmalehuman.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_Hfemalehumanfactory.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_Hihuman.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_Hihumanfactory.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_Hstandardhumanfactory.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_Hwhitefemalehuman.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_Hwhitehuman.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_Hwhitemalehuman.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_Hyellowfemalehuman.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_Hyellowmalehuman.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_Hmalehumanfactory.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_Hmalehumanfactory.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;
}运行结果:
阅读全文
0 0
- c++设计模式五,抽象工厂模式
- 五、抽象工厂模式
- 大话设计模式-----(五)观察者模式、抽象工厂模式
- C#--抽象工厂设计模式原理
- C#--抽象工厂设计模式--三层框架
- 设计模式(五) -- 抽象工厂及各种工厂总结
- 设计模式学习笔记五:抽象工厂模式
- 设计模式系列(五)——抽象工厂模式
- 设计模式(五)——抽象工厂模式
- C++设计模式五--AbstractFactoryPattern(抽象工厂模式)
- 设计模式(五)抽象工厂模式详解
- 设计模式(五)——抽象工厂模式
- 抽象工厂设计模式
- 设计模式-----抽象工厂
- 抽象工厂设计模式
- 抽象工厂设计模式
- 抽象工厂设计模式
- 设计模式 抽象工厂
- vscode 快速生成html
- 第5周 【项目二】 建立链栈算法库
- 职场社交是一个真需求吗?
- 产品经理必备的14大效率工具
- Docker 入门
- c++设计模式五,抽象工厂模式
- 狐友商家怎样运营狐友帐号
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- 云计算_CentOS下用Tomcat+Zookeeper+Nginx+Solr完美搭建SolrCloud平台
- 《腾讯方法》阅后感:让你10分钟读完一本好书
- 「会会」产品体验报告
- 惊!!!Javascript入门必学知识
- 日常作业1
- excel的VBA插图源代码,解决2010版后插图源路径破坏图片不影响问题