Design Pattern: Chain of Responsibility Pattern

Chain of Responsibility: Give multiple objects the opportunity to process the request, thereby avoiding the coupling relationship between the sender and receiver of the request. Make this object into a chain, and pass the request until there is an object to handle it.


Handler class, which defines an interface for handling requests

abstract class Handler{
    protected Handler successor;
    //set successor
    public void SetSuccessor(Handler successor){
        this.successor = successor;
    }
    //Abstract method to handle request
    public abstract void HandleRequest(int request);
}

      The ConcreteHandler class, which specifically handles the class, handles the request it is responsible for, and can access its successor. If the request can be processed, it will be processed, otherwise the request will be forwarded to its successor.

ConcreteHandler1, when the number of requests is between 0 and 10, it has the right to process, otherwise it goes to the next bit.

class ConcreteHandler1 implements Handler{
    public void HandleRequest(int request){
        //0 to 10, handle this request
        if (request >=0 && request < 10) {
            System.out.println("Processing request",this.GetType().Name,request);
        }else if (successor != null) {
            // move to the next bit
            successor.HandleRequest(request);            
        }
    }
}

ConcreteHandler2, when the number of requests is between 10 and 20, it has the right to process, otherwise it goes to the next bit.

class ConcreteHandler2 implements Handler{
    public void HandleRequest(int request){
        //10 to 20, process this request
        if (request >=10 && request < 20) {
            System.out.println("Processing request",this.GetType().Name,request);
        }else if (successor != null) {
            // move to the next bit
            successor.HandleRequest(request);            
        }
    }

}

ConcreteHandler3, when the number of requests is between 20 and 30, it has the right to process, otherwise it goes to the next bit.

class ConcreteHandler3 implements Handler{
    public void HandleRequest(int request){
        //20 to 30, process this request
        if (request >=20 && request < 30) {
            System.out.println("Processing request",this.GetType().Name,request);
        }else if (successor != null) {
            // move to the next bit
            successor.HandleRequest(request);            
        }
    }

}
Client code that submits requests to specific handler objects on the chain.

Handler h1= new ConcreteHandler1();
Handler h2= new ConcreteHandler2();
Handler h3= new ConcreteHandler3();

h1.SetSuccessor(h2);
h2.SetSuccessor(h3);

int[] requests = {2,5,14,22,18,3,27,20};

foreach(int request in request){
    h1.HandleRequest(request);
}
Benefits of Chain of Responsibility
      When a client submits a request, the request is passed along the chain until a ConcreteHandler object is responsible for handling it.

      This leaves neither the receiver nor the sender with explicit information about each other, and the objects in the chain themselves do not know the structure of the chain. The result is that the chain of responsibility simplifies the interconnection of objects, they only need to maintain a reference to their successor , without keeping references to all its candidate recipients.

      The structure that handles a request can be added or modified at any time. Increased flexibility in assigning responsibilities to objects.

      There is a very high chance that a request will not be processed at the end of the chain, or it will not be processed because it is not configured correctly.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642297&siteId=291194637