职责链设计模式解决if else多分支问题
来源:互联网 发布:排雷软件 编辑:程序博客网 时间:2024/05/22 04:40
具体方案具体分析,此代码适合分类业务进行的if else操作,解决多分支问题
//方案一:职责链设计模式#include <iostream>#include <vector>#include <string>#include <string.h>using namespace std;typedef string KV;class ProcessCmdFromWeb{protected:ProcessCmdFromWeb *successor;public:ProcessCmdFromWeb(){successor = NULL;}virtual int ProcessCmd(KV& in, KV& out){cout<<"**"<<endl;return 0;}void SetSuccesssor(ProcessCmdFromWeb *successor){this->successor = successor;}};class ProcessSysResQuery:public ProcessCmdFromWeb{public:int ProcessCmd(KV& in, KV& out){if(strcmp(in.c_str(), "a1") == 0){cout<<"a1"<<endl;}else if(strcmp(in.c_str(), "a2") == 0){cout<<"a2"<<endl;}else if(successor != NULL){successor->ProcessCmd(in, out);}return 0;}};class ProcessPreSeverMsg:public ProcessCmdFromWeb{public:int ProcessCmd(KV& in, KV& out){if(strcmp(in.c_str(), "b1") == 0){cout<<"b1"<<endl;}else if(strcmp(in.c_str(), "b2") == 0){cout<<"b2"<<endl;}else if(successor != NULL){successor->ProcessCmd(in,out);}return 0;}};class ProcessCmdEnd:public ProcessCmdFromWeb{public:int ProcessCmd(KV& in, KV& out){cout<<"not found"<<endl;return 0;}};int main(){ProcessCmdFromWeb *h1 = new ProcessSysResQuery;ProcessCmdFromWeb *h2 = new ProcessPreSeverMsg;ProcessCmdFromWeb *hend = new ProcessCmdEnd;h1->SetSuccesssor(h2);h2->SetSuccesssor(hend);vector<string> str1;str1.push_back("a1");str1.push_back("b2");str1.push_back("b1");vector<string>::iterator iter = str1.begin();for(; iter != str1.end(); iter++){h1->ProcessCmd(*iter,*iter);}delete h1;delete h2;delete hend;system("pause");return 0;}
- 职责链设计模式解决if else多分支问题
- 设计模式之 if-else
- 利用枚举enum解决多if.....else if ..问题
- 你还在用if else吗?——状态模式、职责链模式
- 对于if ...else if .... 分支太多的问题
- if else 多分支 与for循环
- if-else 分支预测
- 策略模式+工厂方法 实现多分支操作(代替多个if else)
- if-else重构与设计模式
- if/else 分支控制语句
- 应用分支结构if...else....
- 职责链设计模式
- 【设计模式】职责链
- 设计模式---职责链
- 职责链设计模式
- 职责链设计模式
- 使用if…else,if…else实现多分支选择结构
- if...else if switch分支结构
- 十月29日
- 【逆序对】序列
- 会话 (cookie)管理
- 单例模式总结
- AIX/Unix/Linux/HP-UX 系统中文字符编码设置
- 职责链设计模式解决if else多分支问题
- 210学习日记(2) --工欲善其事,必先利其器
- Console号令详解,让调试js代码变得更简单
- 汇编10H中断
- jsonp代码
- 【找规律】悟空学艺
- 规范国界图的获取方法
- 210学习日记(3)_支持串口
- ArrayList 和LinkedList各自的特点是什么?