Design Pattern - Chain of Responsibility Pattern

Chain of Responsibility Pattern - Chain of Responsibility Pattern

Chain of Responsibility Pattern: Avoid coupling request senders and receivers, make it possible for multiple objects to receive requests, connect these objects into a chain, and pass requests along this chain until there is a object until it is processed. The Chain of Responsibility pattern is an object behavioral pattern.

Abstract handler : It defines an interface for processing requests, which is generally designed as an abstract class. Since different concrete handlers handle requests in different ways, an abstract request processing method is defined in it. Because the next home of each handler is still a handler, an object of abstract handler type is defined in the abstract handler as its reference to the next home. Through this reference, handlers can be linked together in a chain. 

 Concrete handler : It is a subclass of the abstract handler, which can process user requests. The abstract request processing method defined in the abstract handler is implemented in the concrete handler class. Before processing the request, it needs to be judged to see if there is a corresponding If the request can be processed, it will be processed, otherwise the request will be forwarded to the successor; in the specific processor, the next object in the chain can be accessed for the forwarding of the request.


abstract handler

public abstract class OApprover {
    protected OApprover oApprover;
    protected String name;

    public OApprover(String name) {
        this.name = name;
    }

    public void setoApprover(OApprover oApprover) {
        this.oApprover = oApprover;
    }

    public abstract void processRequest(Integer days);
}
specific processor

public class 主任 extends OApprover {

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

    @Override
    public void processRequest(Integer days) {
        if(days < 3){
            System.out.println("leave"+days+"days, director"+this.name+"approved");
        }else{
            oApprover.processRequest(days);
        }
    }
}
public class manager extends OApprover {
    public manager(String name) {
        super(name);
    }

    @Override
    public void processRequest(Integer days) {
        if(days < 10){
            System.out.println("leave"+days+"days, manager"+this.name+"approved");
        }else{
            oApprover.processRequest(days);
        }
    }
}

public class general manager extends OApprover {

    public general manager(String name) {
        super(name);
    }

    @Override
    public void processRequest(Integer days) {
        if (days > 30){
            System.out.println("The general manager can't approve either");
        }else {
            System.out.println("Leave" + days + "Days, Director" + this.name + "Approved");
        }
    }

}

Client

public class Client {
    public static void main(String[] args) {
        OApprover character 1, character 2, character 3;

        Character 1 = new Director("Xi Da Zhuang");
        Person2 = new manager("Meili Zhang");
        character 3 = new general manager("white fool");

        //create the chain of responsibility
        Character 1.setoApprover(character 2);
        character 2.setoApprover(character 3);

        Character 1.processRequest(2);
        Character 1.processRequest(5);
        Character 1.processRequest(15);
    }
}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852200&siteId=291194637