职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止。
优点 :
- 责任链对象只和自己的后继有耦合关系,与其他的对象没有关系。
- 在职责中,责任链对程序更加灵活。
- 可以动态的增加或删除处理者,也可以改变先后顺序。
- 用户不必考虑处理者的信息。
基本的模型代码:
#include <iostream>
#include <string>
using namespace std;
class Handler
{
public:
Handler(string name)
{
this->name = name;
}
void SetSuccessor(Handler* successor)
{
this->successor = successor;
}
Handler* getSuccessor()
{
return successor;
}
virtual void HandlerRequest(int request) = 0;
protected:
Handler* successor;
string name;
};
class ConcreteHandle1 : public Handler
{
public:
ConcreteHandle1(string name):Handler(name){}
void HandlerRequest(int request)
{
if(request >= 0 && request < 10)
cout << name << "处理请求" << request << endl;
else if (successor != NULL)
successor->HandlerRequest(request);
}
};
class ConcreteHandle2 : public Handler
{
public:
ConcreteHandle2(string name):Handler(name){}
void HandlerRequest(int request)
{
if(request >= 10 && request < 20)
cout << name << "处理请求" << request << endl;
else if (successor != NULL)
successor->HandlerRequest(request);
}
};
class ConcreteHandle3 : public Handler
{
public:
ConcreteHandle3(string name):Handler(name){}
void HandlerRequest(int request)
{
if(request >= 20 && request < 30)
cout << name << "处理请求" << request << endl;
else if (successor != NULL)
successor->HandlerRequest(request);
}
};
int main()
{
Handler* h1 = new ConcreteHandle1("h1");
Handler* h2 = new ConcreteHandle2("h2");
Handler* h3 = new ConcreteHandle3("h3");
h1->SetSuccessor(h2);
h2->SetSuccessor(h3);
int requests[8] = {2,5,14,22,18,3,27,20};
for(int i = 0;i < 8;i++)
{
h1->HandlerRequest(requests[i]);
}
return 0;
}
使用list实现职责链模式
#include <iostream>
#include <string>
#include <list>
using namespace std;
//抽象类
class Handler
{
public:
virtual void handleRequest(string number) = 0;
virtual void setNextHandler(Handler* handler) = 0;
};
//具体处理者1
class Jinan : public Handler
{
public:
Jinan()
{
numberList.push_back("370100199510110021");
numberList.push_back("370100199510110022");
numberList.push_back("370100199510110023");
numberList.push_back("370100199510110024");
}
void handleRequest(string number)
{
list<string>::iterator it;
for(it = numberList.begin();it != numberList.end();it++)
{
if(*it == number)
{
cout << "济南人" << endl;
}
}
if(it == numberList.end())
{
//cout << "非济南人" << endl;
if(NULL != m_Handler)
m_Handler->handleRequest(number);
}
}
void setNextHandler(Handler* handler)
{
m_Handler = handler;
}
private:
Handler* m_Handler;
list<string> numberList;
};
//具体处理者2
class Qidao : public Handler
{
public:
Qidao()
{
numberList.push_back("370201199510110021");
numberList.push_back("370201199510110022");
numberList.push_back("370201199510110023");
numberList.push_back("370201199510110024");
}
void handleRequest(string number)
{
list<string>::iterator it;
for(it = numberList.begin();it != numberList.end();it++)
{
if(*it == number)
{
cout << "青岛人" << endl;
}
}
if(it == numberList.end())
{
//cout << "非青岛人" << endl;
if(NULL != m_Handler)
m_Handler->handleRequest(number);
}
}
void setNextHandler(Handler* handler)
{
m_Handler = handler;
}
private:
Handler* m_Handler;
list<string> numberList;
};
//具体处理者3
class Dezhou : public Handler
{
public:
Dezhou()
{
numberList.push_back("371428199510110021");
numberList.push_back("371428199510110022");
numberList.push_back("371428199510110023");
numberList.push_back("371428199510110024");
}
void handleRequest(string number)
{
list<string>::iterator it;
for(it = numberList.begin();it != numberList.end();it++)
{
if(*it == number)
{
cout << "德州人" << endl;
}
}
if(it == numberList.end())
{
//cout << "非德州人" << endl;
if(NULL != m_Handler)
m_Handler->handleRequest(number);
}
}
void setNextHandler(Handler* handler)
{
m_Handler = handler;
}
private:
Handler* m_Handler;
list<string> numberList;
};
int main()
{
Handler* jinan = new Jinan();
Handler* qidao = new Qidao();
Handler* dezhou = new Dezhou();
jinan->setNextHandler(qidao);
qidao->setNextHandler(dezhou);
jinan->handleRequest("371428199510110024");
return 0;
}