工厂模式--c++实现
来源:互联网 发布:查看端口8080的命令 编辑:程序博客网 时间:2024/06/10 08:24
1、基类与子类 (state_init、state_routing、state_exexute:将要通过工厂生产的子类)
class state
{
public:
virtual void process() = 0;
};
class state_init : virtual public state
{
public:
virtual void process(){
cout << "state_init. process" << endl;
}
};
class state_routing : virtual public state
{
public:
virtual void process(){
cout << "state_routing. process" << endl;
}
};
class state_execute : virtual public state
{
public:
virtual void process(){
cout << "state_execute. process" << endl;
}
};
2、工厂类
class statefactory{
public:
static state* generate_state(const string& operate){
if (operate == "init"){
return new state_init();
}else if (operate == "routing"){
return new state_routing();
}else if (operate == "execute"){
return new state_execute();
}else{
return NULL;
}
}
};
3、应用
int main()
{
state* pinit = statefactory::generate_state("init");
state* proute = statefactory::generate_state("routing");
state* pexecute = statefactory::generate_state("execute");
if (pinit){
pinit->process();
delete pinit;
pinit = NULL;
}else{
cout << "pinit is null" << endl;
}
if (proute){
proute->process();
delete proute;
proute = NULL;
}else{
cout << "proute is null" << endl;
}
if (pexecute){
pexecute->process();
delete pexecute;
pexecute = NULL;
}else{
cout << "pexecute is null" << endl;
}
return 0;
}
- C语言实现工厂模式
- 简单工厂模式C语言实现
- 工厂方法模式C语言实现
- C语言实现抽象工厂模式实例
- 工厂模式(C/C++实现理解)
- 简单工厂模式(C语言实现)
- 工厂函数模式 (C语言实现)
- 抽象工厂模式 (C语言实现)
- 设计模式的实现(C++)------工厂方法模式(FactoryMethod)
- 设计模式的实现(C++)------抽象工厂模式(AbstractFactory)
- 工厂模式-C-工厂方法
- C语言实现《设计模式之禅》之工厂模式——标准工厂
- C语言实现《设计模式之禅》之工厂模式——抽象工厂
- 工厂模式(c++)
- C#-简单工厂模式
- C#-工厂方法模式
- 工厂模式 (C#)
- 【C#】简单工厂模式
- 建一个别人不能碰的无敌文件夹
- 压力测试工具
- 杭电OJ2063 (基础二分图匹配)(月老降临)
- 单点登录实现方法
- Android JSON解析(JSONObject和JSONArray)
- 工厂模式--c++实现
- MyBatis 中调用存储过程的方式
- Java二维码工具类(使用zxing实现,可支持logo)
- QAndroidJniObject类 部分函数使用方式
- java提高篇(十六)-----异常(一)
- 机器学习之决策树1
- HashMap的实现原理
- jsp实现ajax的一个例子
- ccf 无线网络