责任链模式(Chain of Responsibility Pattern)是一种行为型设计模式,它允许多个对象按照顺序处理请求,直到其中一个对象能够处理为止。每个对象都可以决定是否处理请求,以及将请求传递给下一个对象。这种模式可以避免请求的发送者和接收者之间的耦合关系。
以下是一个使用 C++实现责任链模式的示例代码:
#include <iostream>
using namespace std;
class Handler {
public:
virtual void setNext(Handler* handler) = 0;
virtual void handleRequest(int request) = 0;
};
class HandlerA :public Handler {
private:
* nextHandler;
Handlerpublic:
void setNext(Handler* handler) {
= handler;
nextHandler }
void handleRequest(int request) {
if (request >= 0 && request < 10) {
<< "HandlerA handleRequest" << endl;
cout }
else if (nextHandler != nullptr) {
->handleRequest(request);
nextHandler}
}
};
class HandlerB :public Handler {
private:
* nextHandler;
Handlerpublic:
void setNext(Handler* handler) {
= handler;
nextHandler }
void handleRequest(int request) {
if (request >= 10 && request < 20) {
<< "HandlerB handleRequest" << endl;
cout }
else if (nextHandler != nullptr) {
->handleRequest(request);
nextHandler}
}
};
class HandlerC :public Handler {
private:
* nextHandler;
Handlerpublic:
void setNext(Handler* handler) {
= handler;
nextHandler }
void handleRequest(int request) {
if (request >= 20 && request < 30) {
<< "HandlerC handleRequest" << endl;
cout }
else if (nextHandler != nullptr) {
->handleRequest(request);
nextHandler}
}
};
int main() {
* handlerA = new HandlerA();
Handler* handlerB = new HandlerB();
Handler* handlerC = new HandlerC();
Handler->setNext(handlerB);
handlerA->setNext(handlerC);
handlerB->handleRequest(5);
handlerA->handleRequest(15);
handlerA->handleRequest(25);
handlerAdelete handlerA;
delete handlerB;
delete handlerC;
return 0;
}
//HandlerA handleRequest
//HandlerB handleRequest
//HandlerC handleRequest