1、简单工厂模式
enum CTYPE {COREA, COREB};
class SingleCore
{
public:
virtual void Show() = 0;
};
class SingleCoreA: public SingleCore
{
public:
void Show() { cout<<"SingleCore A"<<endl; }
};
class SingleCoreB: public SingleCore
{
public:
void Show() { cout<<"SingleCore B"<<endl; }
};
class Factory
{
public:
SingleCore* CreateSingleCore(enum CTYPE ctype)
{
if(ctype == COREA)
return new SingleCoreA();
else if(ctype == COREB)
return new SingleCoreB();
else
return NULL;
}
};
2、其他工厂
wuzhekai1985:设计模式C++实现(1)——工厂模式