Definition: The responsibility chain model (Chain of responsibility pattern) creates a chain of recipient objects for the request. This pattern gives the type of the request, decoupling the sender and receiver of the request. This type of design pattern belongs to the behavioral pattern. In this pattern, each recipient usually contains a reference to another recipient. If an object cannot process the request, it passes the same request to the next recipient, and so on.
Features:1. Reduce the coupling degree. It will decouple the sender and receiver of the request.
2, simplifies the object. So that the object does not need to know the structure of the chain.
3, enhance the flexibility of assigning responsibilities to objects. Add or remove responsibilities dynamically by changing the order of members within the chain or by mobilizing them.
4. It is convenient to add new request processing classes.
Enterprise-class development and applications in common frameworks: Spring,struts interceptors, servlet filters
Concrete Examples:
/** * Here for the company's leave for example: * 1, sb. Leave (Event) * 2, project leader approval: Less than three days can be approved, more than three days to instruct superiors * 3, department heads for approval: Less than 10 days can be approved, more than 10 days to request superior *4, general manager approval: Less than 30 days can be approved, more than 30 days
public class Demo {public static void main (string[] args) {Event event = new event ("Programmer", 32, "Home Visit");
Leader a = new Groupleader ("John");
Leader B = new Manager ("Dick");
Leader C = new Generalmanager ("Harry");
Designation of responsibility chain mode A.setnextleader (b);
B.setnextleader (c);
A.handler (event);
}/** * Event object, the specific handling event in the chain of responsibility * This is the case of leave of absence/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, backAll the actual responsibility chain objects are inherited from the object/abstract class leader{private String name;
Protected Leader the next responsible object in the nextleader;//chain of responsibility, which is the underlying public Leader (String name) {super () that keeps the chain of responsibility alive;
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, leader/class Groupleader extends leader{public groupleader (String name) {super (name); } protected void Handler (event event) {if (Event.getdays () < 3) {System.out.println ("--------------------Leave bar-------
-------------"); System.out.print ("| Leave person: "+event.getname () +" |
");
System.out.println ("Days to Leave:" +event.getdays () + "days" + "|"); System.out.println ("|
Reason for leave: "+event.getreason () +" | "); System.out.println ("Approver:" +this.getname () + "(group leader), through!"
");
System.out.println (); }else{System.out.println (This.getname () + "(supervisor), no permissions!"
"); This.nextLeader.handler (EveNT);
}}/** * The second object in the leave process, department head/class manager extends leader{public Manager (String name) {super (name); } protected void Handler (event event) {if (Event.getdays () < ten) {System.out.println ("--------------------Leave bar------
--------------"); System.out.print ("| Leave person: "+event.getname () +" |
");
System.out.println ("Days to Leave:" +event.getdays () + "days" + "|"); System.out.println ("|
Reason for leave: "+event.getreason () +" | "); System.out.println ("Approving Person:" +this.getname () + "(supervisor), leave pass!"
");
System.out.println (); }else{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 () <) {System.out.println ("--------------------Leave bar------
--------------"); System.out.print ("| Leave person: "+event.getname () +" |
");
System.out.println ("Days to Leave:" +event.getdays () + "days" + "|"); System.out.println ("| Reason for leave: "+evEnt.getreason () + "|");
System.out.println ("Approving Person:" +this.getname () + "(General manager)");
System.out.println ();
}else{System.out.println ("--------------------leave--------------------"); System.out.print ("| Leave person: "+event.getname () +" |
");
System.out.println ("Days to Leave:" +event.getdays () + "days" + "|"); System.out.println ("|
Reason for leave: "+event.getreason () +" | "); System.out.println ("Approving Person:" +this.getname () + "(General manager), the person who leave does not pass!"
");
System.out.println ();
}
}
}
Responsibility chain mode is mainly used in the development of process control, compared with If-else mode, it has higher efficiency in code modification and integration, and is more advantageous to code maintenance. Furthermore, the need to increase the number of objects in the chain of responsibility requires the realization of a specific responsibility chain class, then create the responsibility object, and maintain the responsibility chain relationship on the line, do not need more overhead, and in the actual design, we often maintain the chain of responsibility in the configuration file, so we will save more development time.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.