Comic design pattern: What is the "chain of responsibility pattern"?

----- the next day -----


————————————

In a company, there are three programmers whose names are Little A, Little B, and Little C:

One day, the company’s new product manager had a new requirement, but she did not know who should be responsible for this requirement. So, she first found little A:

So, the product manager found little B again:

So, the product manager found little C again:

In this way, a task is transferred from small A to small B, from small B to small C, and finally processed by small C, forming a complete task processing chain:

In the above chain, there are different task handlers. Faced with a new task, each task processor needs to judge whether it can handle the task, if it can handle it, then handle it and return; if it can’t handle it, transfer it to the next task processor until a certain task processor finally completes deal with. This is the core idea of ​​the chain of responsibility model .


abstract public class Handler {

    protected Handler successor;

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }

    abstract String handleRequest(String msg);
}

As can be seen from the above abstract class, each Handler object contains a successor member, which points to its next task handler, just like the next pointer of the linked list node.

public class HandlerA extends Handler {
    @Override
    String handleRequest(String msg) {
        if(msg.contains("a")){
            msg = msg.replace('a', '*');
        } else if(this.successor != null){
            msg = this.successor.handleRequest(msg);
        }
        return msg;
    }
}

public class HandlerB extends Handler {
    @Override
    String handleRequest(String msg) {
        if(msg.contains("b")){
            msg = msg.replace('b', '*');
        } else if(this.successor != null){
            msg = this.successor.handleRequest(msg);
        }
        return msg;
    }
}

public class HandlerC extends Handler {
    @Override
    String handleRequest(String msg) {
        if(msg.contains("c")){
            msg = msg.replace('c', '*');
        } else if(this.successor != null){
            msg = this.successor.handleRequest(msg);
        }
        return msg;
    }
}

In these three Handler implementation classes, similar judgments are made:

If the incoming message string contains a certain letter, replace the corresponding letter with *. Once a Handler replaces the letter it is responsible for, it directly ends the entire link; if there is no letter it is responsible for, the next Handler is designated to continue processing.

public class Client {

    public static void main(String[] args) {
        Handler handlerA = new HandlerA();
        Handler handlerB = new HandlerB();
        Handler handlerC = new HandlerC();

        handlerA.setSuccessor(handlerB);
        handlerB.setSuccessor(handlerC);

        System.out.println(handlerA.handleRequest("apple"));
        System.out.println(handlerA.handleRequest("bicycle"));
        System.out.println(handlerA.handleRequest("color"));
    }
}

In the client code, you can flexibly set the order of the entire link and the processor, and then directly call the handleRequest method of the first processor, which is equivalent to starting the entire link.


Friends who have done web development know that when a client sends an HTTP request to a web application, it will first pass through the layer of filters (Filter) of the Tomcat container . The filter will be based on the requested access rights, parameter legality, etc. Aspects are verified and filtered.

The realization of this layer of filters uses the chain of responsibility model.

Friends who are familiar with the SpringMVC framework source code should all know that after the client's HTTP request arrives in the web application, it will be distributed by the DispatcherServlet class of the SpringMVC framework and distributed to the specific methods of the Controller layer.

Before entering the business logic of the Controller layer and after executing the business logic, the request will go through a series of Interceptors . The processing flow of this series of interceptors is also the realization of the chain of responsibility model.

—————END—————

Friends who like this article, welcome to follow the official account  programmer Xiaohui , and watch more exciting content

点个[在看],是对小灰最大的支持!

Guess you like

Origin blog.csdn.net/bjweimengshu/article/details/110016220