Study and summary of Dahua design mode duty chain mode

1, Concept

Responsibility chain pattern: allows multiple objects to process requests, thus avoiding the coupling between the sender and the receiver of the request. Join the object into a chain and pass the request along the chain until an object accepts it.

2, Class diagram and basic code

//Interface for handling requests
public abstract class Handler {
    protected Handler successor;
    //Set up a successor
    public void SetSuccessor(Handler successor){
        this.successor=successor;
    }
    //Process request
    public abstract void HandleRequest(int request);


}
//Specific handler, handle the request it is responsible for, and access its successor,
//If the request can be processed, process it, otherwise forward the request to its successor


//Right to deal with 0-10
public class ConcreteHandler1 extends Handler{

    @Override
    public void HandleRequest(int request) {
        if(request>=0&&request<10){
            System.out.println(this.getClass().getName()+"Process request"+request);
        }else if(successor!=null){
            successor.HandleRequest(request);
        }
    }

}
//Right to deal with 10-20
public class ConcreteHandler2 extends Handler{

    @Override
    public void HandleRequest(int request) {
        if(request>=10&&request<20){
            System.out.println(this.getClass().getName()+"Process request"+request);
        }else if(successor!=null){
            successor.HandleRequest(request);
        }
    }

}
//Right to deal with within 20-30
public class ConcreteHandler3 extends Handler{

    @Override
    public void HandleRequest(int request) {
        if(request>=20&&request<30){
            System.out.println(this.getClass().getName()+"Handle"+request);
        }else if (successor!=null) {
            successor.HandleRequest(request);
        }
    }

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

        handler1.SetSuccessor(handler2);
        handler2.SetSuccessor(handler3);


        int requests[]=new int[]{
                2,5,14,22,18,3,27,20
        };
        for (int i : requests) {
            handler1.HandleRequest(i);
        }
    }

}

3, Example leave and salary increase

//Manager (Abstract)
public abstract class Manager {
    protected String name;
    protected Manager superior;
    public Manager(String name){
        this.name=name;
    }

    //Set up the manager's Supervisor
    public void SetSuperior(Manager superior){
        this.superior=superior;
    }

    //Process request
    public abstract void RequestApplications(Request request);
}
//manager
public class CommonManager extends Manager{

    public CommonManager(String name) {
        super(name);
    }

    @Override
    public void RequestApplications(Request request) {
        if(request.getRequestType()=="leave"&&request.getNumber()<=2){
            System.out.println(name+"Approval"+request.getRequestType()+"  Number:"+request.getNumber());
        }else if(superior!=null){
            superior.RequestApplications(request);
        }
    }

}
//general manager
public class GeneralManager extends Manager{

    public GeneralManager(String name) {
        super(name);
    }

    @Override
    public void RequestApplications(Request request) {
        if(request.getRequestType()=="leave"){
            System.out.println(name+"Approval"+request.getRequestType()+"  Number:"+request.getNumber());
        }else if(request.getRequestType()=="Pay rise"&&request.getNumber()<=500){
            System.out.println(name+"Approval"+request.getRequestType()+"  Number:"+request.getNumber());
        }else if(request.getRequestType()=="Pay rise"&&request.getNumber()>500){
            System.out.println(name+"Disapproval"+request.getRequestType()+" Number:"+request.getNumber());
        }
    }

}
//Chief inspector
public class Majordomo extends Manager{

    public Majordomo(String name) {
        super(name);
    }

    @Override
    public void RequestApplications(Request request) {
        if(request.getRequestType()=="leave"&&request.getNumber()<=5){
            System.out.println(name+"Approval"+request.getRequestType()+"  Number:"+request.getNumber());
        }else if(superior!=null){
            superior.RequestApplications(request);
        }
    }

}
//Apply
public class Request {
    //Application type
    private String requestType;
    //Application content
    private String requestContent;
    //Number
    private int number;
    public String getRequestType() {
        return requestType;
    }
    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }
    public String getRequestContent() {
        return requestContent;
    }
    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }




}
/*
 * The client 's application is started by manager a, and the client does not know who will handle it
 */
public class Client {
    public static void main(String[] args) {
        CommonManager a=new CommonManager("a");
        CommonManager b=new CommonManager("b");
        CommonManager c=new CommonManager("c");

        a.SetSuperior(b);
        b.SetSuperior(c);


        Request request=new Request();
        request.setRequestType("leave");
        request.setRequestContent("Asking for leave");
        request.setNumber(1);

        a.RequestApplications(request);

        Request request2=new Request();
        request2.setNumber(4);
        request2.setRequestType("leave");
        request2.setRequestContent("Asking for leave");

        a.RequestApplications(request2);


        Request request3=new Request();
        request3.setNumber(500);
        request3.setRequestType("Pay rise");
        request3.setRequestContent("Ask for a raise");

        a.RequestApplications(request3);


        Request request4=new Request();
        request4.setNumber(1000);
        request4.setRequestType("Pay rise");
        request4.setRequestContent("Ask for a raise");

        a.RequestApplications(request4);
    }

}

4, Summary

1. Benefits. By reducing the coupling degree of the system, we can simplify the connection between objects. We only need to save a reference to its successor, not to save its reference to all candidate recipients. The flexibility of assigning responsibility to objects is enhanced. The responsibility of processing a request can be increased or changed by adding or modifying the responsibility chain.
2. Timing of use. When there are multiple objects that can handle the same request, but the specific object handling the request is automatically determined by the runtime, that is, the specific recipient of the request is not clear.

The path and dream is up to you to weave.

Keywords: supervisor

Added by copernic67 on Tue, 05 May 2020 02:02:22 +0300