Definition: The Chain of Responsibility Pattern creates a chain of the receiver object for the request. This pattern gives the type of request, decoupling the sender and receiver of the request. This type of design pattern belongs to the behavioral pattern. In this mode, usually each recipient contains a reference to another recipient. If an object cannot process the request, it passes the same request to the next receiver, and so on.
Features: 1. Reduce coupling degree. It decouples the sender and receiver of the request.
2. Simplified the object. This makes the object not necessary to know the structure of the chain.
3. Enhance the flexibility to assign responsibilities to the target. By changing the members in the chain or mobilizing their order, it allows for dynamic addition or deletion of responsibilities.
4. It is very convenient to add new request processing classes.
Applications in enterprise-level development and commonly used frameworks: spring, struts interceptor, servlet filter
Specific examples:
/** * Here is an example of a company's leave: *1. Someone takes leave (event) *2. Project team leader's approval: Approval can be approved less than three days, and requests superiors more than three days*3. Department supervisor's approval: Approval can be approved less than ten days, and requests superiors more than ten days*4. General manager's approval: Approval can be approved less than 30 days, and does not approve more than 30 days*/public class Demo { public static void main(String[] args) { Event event = new Event("Programmer", 32, "Return to hometown to visit relatives"); Leader a = new GroupLeader("Zhang San"); Leader b = new Manager("Li Si"); Leader c = new GeneralManager("Wang Wu"); //Specify the chain of responsibility mode a.setNextLeader(b); b.setNextLeader(c); a.handler(event); }}/** * Event object, specific processing events in the chain of responsibility* This instance is the matter of asking for leave*/class Event{ private String name; private int days; private String reason; public Event(String name, int days, String reason) { super(); this.name = name; this.days = days; this.reason = reason; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; }}/** * Abstract responsibility chain object, all the actual responsibility chain objects following are inherited from this object*/abstract class Leader{ private String name; protected Leader nextLeader;//The next responsibility object in the chain of responsibility, this is the basis for keeping the chain of responsibility public Leader(String name) { super(); this.name = name; } public void setNextLeader(Leader nextLeader) { this.nextLeader = nextLeader; } public String getName() { return name; } public void setName(String name) { this.name = name; } protected abstract void handler(Event event);}/** * The first object in the leave process, group leader*/class GroupLeader extends Leader{ public GroupLeader(String name) { super(name); } protected void handler(Event event) { if(event.getDays() < 3){ System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Reason for leave: "+event.getReason() + " |"); System.out.println("Approver: "+this.getName()+" (group leader), pass! "); System.out.println(); }else{ System.out.println(this.getName()+"(supervisor), no permissions!"); this.nextLeader.handler(event); } }}/** * The second object in the leave process, department manager*/class Manager extends Leader{ public Manager(String name) { super(name); } protected void handler(Event event) { if(event.getDays() < 10){ System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println(this.getName()+"(General Manager), no permissions!"); this.nextLeader.handler(event); } }}/** * The third object in the leave process, General Manager*/class GeneralManager extends Leader{ public GeneralManager(String name) { super(name); } protected void handler(Event event) { if(event.getDays() < 30){ System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ offender: "+event.getName()+" | "); System.out.println("Number of leave days: "+event.getDays()+"days"+" |"); System.out.println("| Reason for leave: "+event.getReason() + " |"); System.out.println("Approver: "+this.getName()+" (General Manager)"); System.out.println(); }else{ System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The leave requester: "+event.getName()+" | "); System.out.println("Number of leave days: "+event.getDays()+" days"+" |"); System.out.println("| Reason for leave: "+event.getReason() + " |"); System.out.println("Approval: "+this.getName()+" (General Manager), the leave requester fails to pass! "); System.out.println(); } }}The responsibility chain model is mainly used for development such as process control. Compared with the if-else model, it has higher efficiency in code modification and integration, which is more conducive to code maintenance. Moreover, when it is necessary to add objects in the responsibility chain, we need to implement a specific responsibility chain class, then create a responsibility object, and maintain the responsibility chain relationship, without greater overhead. Moreover, in actual design, many times, the maintenance of the responsibility chain is carried out in the configuration file, which will save more development time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.