职责链设计模式解决if else多分支问题

具体方案具体分析,此代码适合分类业务进行的if else操作,解决多分支问题


//方案一:职责链设计模式
#include 
#include 
#include 
#include 
using namespace std;

typedef string KV;

class ProcessCmdFromWeb
{
protected:
	ProcessCmdFromWeb *successor;

public:
	ProcessCmdFromWeb()
	{
		successor = NULL;
	}
	virtual int ProcessCmd(KV& in, KV& out)
	{
		cout<<"**"<successor = successor;
	}
};

class ProcessSysResQuery:public ProcessCmdFromWeb
{
public:
	int ProcessCmd(KV& in, KV& out)
	{
		if(strcmp(in.c_str(), "a1") == 0)
		{
			cout<<"a1"<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"<ProcessCmd(in,out);
		}
		return 0;
	}
};

class ProcessCmdEnd:public ProcessCmdFromWeb
{
public:
	int ProcessCmd(KV& in, KV& out)
	{
		cout<<"not found"<SetSuccesssor(h2);
	h2->SetSuccesssor(hend);

	vector str1;
	
	str1.push_back("a1");
	str1.push_back("b2");
	str1.push_back("b1");

	vector::iterator iter = str1.begin();
	for(; iter != str1.end(); iter++)
	{
		h1->ProcessCmd(*iter,*iter);
	}
	
	delete h1;
	delete h2;
	delete hend;

	system("pause");
	return 0;
}


你可能感兴趣的:(设计模式)