A, definitions,

The Chain of Responsibility Pattern creates a Chain of recipient objects for a request. This pattern decouples the sender and receiver of the request by giving the type of request. This type of design pattern is behavioral. In this pattern, each receiver typically contains a reference to the other receiver. If an object cannot handle the request, it passes the same request to the next recipient, and so on. In general, you can implement the data structure of the responsibility chain pattern through a data list.

The responsibility chain pattern mainly consists of the following roles.

  • The role of Abstract Handler: Defines an interface to process a request, including abstract processing methods and a subsequent connection.
  • The role of Concrete Handler: Implements the processing method of the abstract Handler, determines whether the request can be processed, if it can be processed, and otherwise passes the request to its successor.
  • The Client role: Creates a processing chain and submits a request to a specific handler object in the header. It does not care about processing details or the delivery of the request.

The essence of responsibility chain mode is to decouple request from processing so that the request can be transmitted and processed in the processing chain. Understanding the chain of responsibility model should understand its pattern, not its implementation. The uniqueness of the chain of responsibility mode is that it combines its node handlers into a chain structure and allows the node itself to decide whether to process or forward the request, which is equivalent to letting the request flow.

2. Application scenarios of responsibility chain mode

In the real world, it is common for an event to be processed by multiple objects. For example, procurement approval process, leave process and so on. Company staff leave, can batch of false with the leadership of the department, deputy general manager, general manager, director and so on, but each leader to approve the number of days, the employee must leave according to need the number of days to find different signatures of leadership, that is to say, employees must remember the information such as name, phone number and address of each led, it increases the difficulty.

// Abstract the handler role
public abstract class Handler {
    private Handler next;
    public void setNext(Handler next) {
        this.next = next;
    }
    public Handler getNext(a) {
        return next;
    }
    // How to handle requests
    public abstract void handleRequest(String request);
}
Copy the code
// Specify handler role 1
public class ConcreteHandler1  extends Handler {
    public void handleRequest(String request) {
        if (request.equals("one")) {
            System.out.println(Handler 1 is responsible for processing the request!);
        } else {
            if(getNext() ! = null) { getNext().handleRequest(request); }else {
                System.out.println("There is no one on handler 1 to process the request!"); }}}}Copy the code
// Specify handler role 2
public class ConcreteHandler2  extends Handler {
    public void handleRequest(String request) {
        if (request.equals("two")) {
            System.out.println("Handler 2 is responsible for processing the request!");
        } else {
            if(getNext() ! = null) { getNext().handleRequest(request); }else {
                System.out.println("There is no one on handler 2 to process the request!"); }}}}Copy the code

Third, summary

Advantages:

1) You can control the order in which requests are processed. 2) Single responsibility principle. You can decouple the class that initiates the operation from the class that performs it. 3) Open and close principle. You can add handlers to your program without changing the existing code. Disadvantages: 1) When assembling the chain, it is necessary to pay attention to the situation of the current chain node, whether to execute the next node, otherwise the request will not be processed by the subsequent responsibility chain.

Add: The chain of responsibility model is usually applied in conjunction with the Builder model

Git source: github.com/ljx958720/d…

If this article is helpful to you, welcome to like and forward, but also welcome to talk about their own experience in learning, convenient for everyone to learn and grow together! Read more technical dry articles on Java Architecture.