Talk programming - responsibility chain mode

Keywords: Programming

Chain of Responsibility Pattern

It gives multiple objects the opportunity to process the request, thus avoiding the coupling between the sender and the receiver of the request. Connect these objects into a chain, and pass the request along the chain until there is an object to handle it.

motivation

Each processing object determines which command objects it can handle, and it also knows how to pass command objects it cannot handle to the next processing object in the chain. Give multiple objects an opportunity to process a request, thus decoupling the sender and the receiver. The request is passed along the object chain until it is processed by one of the objects.

Use scenario

  • There are multiple objects that can handle a request, and the running time of which object handles the request is automatically determined.

  • You want to submit a request to one of multiple objects without specifying the recipient explicitly.

  • A collection of objects that can handle a request should be specified dynamically.

structure

Code example

//Abstract class for processing requests
public abstract class Handler {

    protected Handler successor;

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }
    
    public abstract void HandleRequest(int i);
}
//Specific handler
public class SparnHandler extends Handler {
    @Override
    public void HandleRequest(int i) {
        if (i < 30) {
            System.out.println("Sparn To deal with");
        } else {
            successor.HandleRequest(i);
        }
    }
}
//Specific handler
public class FanHandler extends Handler {
    @Override
    public void HandleRequest(int i) {
        if (i < 60) {
            System.out.println("Fan To deal with");
        } else {
            successor.HandleRequest(i);
        }
    }
}
//Specific handler
public class ComplalnHandler extends Handler {
    @Override
    public void HandleRequest(int i) {
        if (i < 80) {
            System.out.println("Complaln To deal with");
        } else {
            successor.HandleRequest(i);
        }
    }
}
//Specific handler
public class NewLocHandler extends Handler {
    @Override
    public void HandleRequest(int i) {
        if (i < 99) {
            System.out.println("NewLoc To deal with");
        } else {
            successor.HandleRequest(i);
        }
    }
}
public class Client{
    public static void main(String[] args) {
        Handler sparn = new SparnHandler();
        Handler fan = new FanHandler();
        Handler complaln = new ComplalnHandler();
        Handler newLoc = new NewLocHandler();
        
        //Specify the next handler, and add an additional handler to ensure that each request can be processed
        sparn.setSuccessor(fan);
        fan.setSuccessor(complaln);
        complaln.setSuccessor(newLoc);

        sparn.HandleRequest(50);
    }
}

: Fan to handle

summary

This pattern makes it unnecessary for an object to specify which other object handles the request. Object only needs to know if the request will be processed correctly. Both the receiver and the sender have no clear information about each other, and the objects in the chain do not need to know the structure of the chain. The chain of responsibility simplifies the interconnection of objects. They only need to maintain a reference to their successors, rather than the application of all its candidates.

Welcome to personal blog

Posted by leo.bonnafe on Sun, 31 May 2020 00:40:36 -0700