1、工厂模式简介
工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中,核心工厂类不在负责产品的创建,这样
核心类成为一个抽象工程角色,仅负责具体工程子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以是系统在不
修改具体工厂角色的情况下引进新的产品。
2、工厂模式1
工厂模式1中的Factory类似于一个真正意义上的工厂(用于生产产品),其结构示意图如下:
模式1通过Factory直接创建不同的产品
3、工厂模式2
结构示意图如下所示:
模式2Factory中只是提供了对象创建的接口,其实现将放在Factory的子类ConcreteFactory中进行。
4、示例
女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了。这个故事是说,女娲在补了天后,下到凡间一看,哇塞,风景太优美了,天空是湛蓝的,水是清澈的,空气是清新的,太美丽了,然后就待时间长了就有点寂寞了,没有动物,这些看的到都是静态的东西呀,怎么办?
别忘了是神仙呀,没有办不到的事情,于是女娲就架起了八卦炉(技术术语:建立工厂)开始创建人,具体过程是这样的:先是泥巴捏,然后放八卦炉里烤,再扔到地上成长,但是意外总是会产生的:第一次烤泥人,兹兹兹兹~~,感觉应该熟了,往地上一扔,biu~,一个白人诞生了,没烤熟!
第二次烤泥人,兹兹兹兹兹兹兹兹~~,上次都没烤熟,这次多烤会儿,往地上一扔,嘿,熟过头了,黑人哪!
第三次烤泥人,兹~兹~兹~,一边烤一边看着,嘿,正正好,Perfect!优品,黄色人类!
这个过程还是比较有意思的,先看看类图:
工程目录如下:
5、代码分析
1)Man
(1)IHuman.h
#ifndef Factory_IHuman_h
#define Factory_IHuman_h
class IHuman
{
public:
IHuman(void)
{
}
virtual ~IHuman(void)
{
}
virtual void Laugh() = 0;
virtual void Cry() = 0;
virtual void Talk() = 0;
};
#endif
2)
//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::Laugh()
{
cout << "黄种人笑" << endl;
}
void CYellowHuman::Cry()
{
cout << "黄种人哭" << endl;
}
void CYellowHuman::Talk()
{
cout << "黄种人说" << endl;
}
3)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;
}
4)BlackHuman
#ifndef __Factory__BlackHuman__
#define __Factory__BlackHuman__
//BlackHuman.h
#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;
}
(2)FactoryProduct
1)IHumanFactory.h
#ifndef Factory_IHumanFactory_h
#define Factory_IHumanFactory_h
#include "IHuman.h"
#include <iostream>
class IHumanFactory
{
public:
IHumanFactory(void)
{
}
virtual ~IHumanFactory(void)
{
}
virtual IHuman* CreateHuman() = 0;
};
#endif
2)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()
{
return new CYellowHuman();
}
3)WhiteHumanFactory
//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();
}
4)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(void)
{
return new CBlackHuman();
}
(3)main.cpp
#include <iostream>
#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"
using std::cout;
using std::endl;
using std::string;
void DoYellowFactory()
{
cout << "黄种人工厂生产黄种人" << endl;
IHumanFactory* pHumanFactory = new CYellowHumanFactory();
IHuman* pHuman = pHumanFactory->CreateHuman();
pHuman->Laugh();
pHuman->Cry();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
void DoWhiteFactory()
{
cout << "白种人工厂生产白种人" << endl;
IHumanFactory* pHumanFactory = new CWhiteHumanFactory();
IHuman* pHuman = pHumanFactory->CreateHuman();
pHuman->Laugh();
pHuman->Cry();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
void DoBlackFactory()
{
cout << "黑种人工厂生产黑种人" << endl;
IHumanFactory* pHumanFactory = new CBlackHumanFactory();
IHuman* pHuman = pHumanFactory->CreateHuman();
pHuman->Laugh();
pHuman->Cry();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
int main(int argc, const char * argv[]) {
cout << "-------------开始生产人-------------" << endl;
cout << "-------------生产黄种人-------------" << endl;
DoYellowFactory();
cout << "-------------生产百种人-------------" << endl;
DoWhiteFactory();
cout << "-------------生产黑种人-------------" << endl;
DoBlackFactory();
return 0;
}