Chain of Responsibility Pattern of C++ Design Pattern

Many students who are new to the design pattern have a little understanding of the chain of responsibility pattern. Today, I will take C++ as an example to make a simple sharing.

The Chain of Responsibility pattern means that multiple objects have more opportunities to process requests, avoiding the coupling relationship between request senders and receivers. Connect these processed objects into a chain, and process the request along the chain. After receiving the request, each processor can process the request, or pass it to the next processor in the chain.

The Chain of Responsibility pattern can be implemented in the following ways:

  1. Create an abstract handler class or interface that contains a method to handle the request;
  2. Create multiple concrete handler classes that will implement the handler interface and define their own processing logic;
  3. Create a requester class that contains a chain of responsibility that can pass the request to each handler on the chain;
  4. Define a send request method in the requester class that passes the request to each handler in the chain of responsibility until a handler is able to handle the request.

Without further ado, let's go to the code:

#include <iostream>  
#include <list>  
using namespace std;  
  
// 抽象处理者类  
abstract class Handler {  
public:  
    virtual void handleRequest(Request request) = 0;  
};  
  
// 具体处理者类1  
class ConcreteHandler1 : public Handler {  
public:  
    void handleRequest(Request request) {  
        if (request == "request1") {  
            cout << "ConcreteHandler1 handles request1." << endl;  
        } else {  
            nextHandler->handleRequest(request);  
        }  
    }  
};  
  
// 具体处理者类2  
class ConcreteHandler2 : public Handler {  
public:  
    void handleRequest(Request request) {  
        if (request == "request2") {  
            cout << "ConcreteHandler2 handles request2." << endl;  
        } else {  
            nextHandler->handleRequest(request);  
        }  
    }  
};  
  
// 请求者类  
class Requester {  
public:  
    Requester(Handler* handler) {  
        handlerList.push_back(handler);  
    }  
  
    void sendRequest(Request request) {  
        handlerList.front()->handleRequest(request);  
    }  
  
private:  
    list<Handler*> handlerList;  
};  
  
// 测试代码  
int main() {  
    // 创建处理者对象  
    ConcreteHandler1 handler1;  
    ConcreteHandler2 handler2;  
  
    // 创建请求者对象并设置责任链  
    Requester requester(&handler1);  
    handler1.nextHandler = &handler2;  
  
    // 发送请求并传递给责任链上的处理者  
    requester.sendRequest("request1");  
    requester.sendRequest("request2");  
    requester.sendRequest("request3");  
  
    return 0;  
}

The above code defines an abstract handler class ​Handler​​​​​, and two concrete handler classes ​ConcreteHandler1​​​​​and ​ConcreteHandler2​​​​​. We also define a requester class ​Requester​, which contains a chain of responsibility that can pass the request to each handler in the chain. In the test code, we created the handler object and the requester object, and set the chain of responsibility to ​ConcreteHandler1​and ​ConcreteHandler2​. Then, we send three requests and observe the output.

When to Use the Chain of Responsibility Pattern?

When it is necessary to send a request to multiple objects for processing or to dynamically specify the set of objects that process the request, if the request is sent to one or more of the multiple objects without specifying the receiver, you can use responsibility chain mode. The Chain of Responsibility pattern decouples the sender and receiver, improving the flexibility and scalability of the system.

Guess you like

Origin blog.csdn.net/renhui1112/article/details/131842322