This is the 27th day of my participation in Gwen Challenge

Chain of responsibility is an object behavior pattern.

Chain of responsibility pattern definition

Chain of responsibility pattern: Avoid coupling the request sender with the receiver, make it possible for multiple objects to receive the request, connect the objects into a chain, and pass the request along the chain until an object handles it.

In the chain of responsibility pattern, many objects are connected in a chain by each object’s reference to its next parent. Requests pass along the chain until one of the objects on the chain decides to process the request. The client making the request does not know which object on the chain ultimately handles the request, allowing the system to dynamically reorganize the chain and assign responsibilities without affecting the client. Filters in servlets, for example, have multiple layers, which, of course, are used more fully in Spring Security.

In the chain of responsibility mode, the client making the request does not know which of these objects will ultimately process the request, so that changes to the system can dynamically reorganize and assign responsibilities without affecting the client.

The structure of the chain of responsibility model

In the Chain of Responsibility pattern, the main roles are Handler and ConcreteHandler:

  • HandlerDefine an interface to handle requests;
  • ConcreteHandlerThe concrete processing class handles the requests it is responsible for, and can access its successors. If the request can be processed, it is processed, otherwise it is forwarded to its successors.

Realization of responsibility chain mode

The abstract class Handler, from which all concrete implementations inherit. Defines the successor, and how to handle the request.

@Data
public abstract class Handler {

    protected Handler successor;/ / successor
    // Process the request
    public void handleRequest(int num) {}}Copy the code

The first successor, num less than 10, is processed, otherwise continue.

public class ConcreteHandler1 extends Handler {

    public void handleRequest(int num) {
        if (num >= 0 && num < 10) {
            System.out.println(this.getClass() + "Processing requests" + num);
        } else if(successor ! =null) { successor.handleRequest(num); }}}Copy the code

The second successor handles num of 10 to 20.

public class ConcreteHandler2 extends Handler {

    public void handleRequest(int num) {
        if (num >= 10 && num < 20) {
            System.out.println(this.getClass() + "Processing requests" + num);
        } else if(successor ! =null) { successor.handleRequest(num); }}}Copy the code

The third successor, dealing with num 20 to 30.

public class ConcreteHandler3 extends Handler {

    public void handleRequest(int num) {
        if (num >= 20 && num < 30) {
            System.out.println(this.getClass() + "Processing requests" + num);
        } else if(successor ! =null) { successor.handleRequest(num); }}}Copy the code

Client sample code. The call chain is: 1->2->3.

public class TestDemo {

    public static void main(String[] args) {
        Handler handler1 = new ConcreteHandler1();
        Handler handler2 = new ConcreteHandler2();
        Handler handler3 = new ConcreteHandler3();

        // Set up the chain of responsibility before and after
        handler1.setSuccessor(handler2);
        handler2.setSuccessor(handler3);

        handler1.handleRequest(15); }}Copy the code

conclusion

This paper mainly introduces the responsibility chain mode of behavior mode. After seeing some articles dealing with a lot of if… else… In addition, the priority order of these processing classes can be arbitrarily set, and if you want to add a new handler class is also very simple, which is in accordance with the open and closed principle. However, care should be taken to avoid circular references in the chain of responsibility. The advantages and disadvantages are summarized as follows:

Advantages:

  • Reduce coupling. It decouples the sender and receiver of the request.
  • Simplifies the object. The object does not need to know the structure of the chain. It is convenient to add new request handling classes.
  • Enhanced flexibility in assigning responsibilities to objects. Responsibilities can be dynamically added or removed by changing members in the chain or reordering them.

Disadvantages:

  • There is no guarantee that the request will be received.
  • System performance will be affected, possibly resulting in circular calls. Runtime characteristics may not be easily observed, hindering debugging.