This article continues to introduce23 Design Mode Seriesmode of responsibility chain.
What is a chain1, chain is a series of nodes of the collection. 2. Each node of the chain can be flexibly split and re-reorganized.
Responsibility Chain ModelEnables multiple objects to have the opportunity to process the request, thereby avoiding the coupling between the sender and the recipient of the request, connecting the object to a chain, and passing the request along the chain until an object has processed him.
roleAbstract processor Role (Handler): Defines an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the home. This role is typically implemented by a Java abstract class or Java interface. Specific processor role (Concretehandler): When a specific processor receives a request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds references to the homes, the specific processor can access the homes if necessary.Abstract Processor Role
Public abstract class Handler { /** * Holds successor to the object of responsibility */ protected Handler successor; /** * Signals the method of processing the request, although the schematic method is not passed in the parameters of the * but actually can pass in parameters, according to the specific needs to choose whether to pass the parameters */public abstract void HandleRequest (); /** * Value method * /public Handler getsuccessor () { return successor; } /** * Assignment method, set successor object of responsibility * /public void Setsuccessor (Handler successor) { this.successor = successor; } }
Specific Processor Roles
public class Concretehandler extends Handler { /** * Processing method, call this method to process the request */ @Override public Void HandleRequest () { /** * Determines if there is a successor to the object of responsibility * If there is, forward the request to the successor of the responsible object * If not, then process the request */if (Getsuccessor ( ) = null) { System.out.println ("Leave Request"); Getsuccessor (). HandleRequest (); } else { System.out.println ("Processing request"); } }}
Client Class
public class Client {public static void Main (string[] args) { //assemble responsibility chain Handler handler1 = new Concretehandler ( ); Handler handler2 = new Concretehandler (); Handler1.setsuccessor (handler2); Submit Request Handler1.handlerequest (); }}
as you can see, the client creates two processor objects and specifies that the first handler object is the second handler object, and the second handler object does not have a next one. The client then passes the request to the first handler object. because the delivery logic of this example is very simple: as long as there is a local, it is passed to the treatment of the under, if not the following, self-treatment. therefore, after the first handler object receives the request, it passes the request to the second handler object. Because the second handler object does not have a next-of-a-person, it processes the request. The activity timing diagram is shown below.
ApplicationApplication for dinner fee management, the general process of applying for a meal fee is generally, the applicant first fill out the application form, and then handed over to the leadership for approval, if the application approved, the leader will inform the applicant approval, and then the applicant to finance the cost, if not approved, the leadership will inform the applicant approval failed, this matter also forget this. Different levels of leadership, the amount of approval is not the same, for example, the project manager can only approve the application within 500 yuan, the department manager can approve the application within 1000 yuan, and the general manager can review any amount of applications.
When a person submits a request for a meal fee application, the request is processed by a leader of the project manager, the department manager, and the general manager, but the person making the application does not know who will ultimately handle his request, and the general applicant submits his application to the project manager, Perhaps finally, the general manager will deal with his request. As long as the applicant directly interacts with the project manager can, the rest of the work in the black box, what the process is, and finally who approved the approval, the applicant does not care.
Abstract Processor Role class
Public abstract class Handler { /** * holds the next object processing request */ protected Handler successor = NULL; /** * Value method * /public Handler getsuccessor () { return successor; } /** * Set the next object to process the request * /public void Setsuccessor (Handler successor) { this.successor = successor; * /** * for processing meal fee * @param user Applicant * @param the amount of money requested by fee * @return specific notice of success or failure * /Public abstract string Handlefeerequest (string user, double fee);}
Specific processor roles
public class Projectmanager extends Handler {@Override public string handlefeerequest (string user, double fee) { String str = ""; Project manager permissions are relatively small, only within 500 if (fee < 500) {//In order to test, simple point, only agree to Zhang San request if ("Zhang San". Equals (user)) {str = "SUCCESS: The project manager agrees with" + user + "" The meal fee, the amount is "+ fee +" Yuan "; }else {//Others do not agree with str = "Failure: The project manager does not agree with" + user + "" The meal fee, the amount is "+ fee +" Yuan "; }}else {//More than 500, continues to pass to higher-level people handling if (getsuccessor ()! = null) { Return Getsuccessor (). Handlefeerequest (user, fee); }} return str; }}
public class Deptmanager extends Handler {@Override public string handlefeerequest (string user, double fee) {STR ing str = ""; Department manager's permissions can only be within 1000 if (fee < 1000) {//In order to test, simple point, only agree to Zhang San request if ("Zhang San". Equals (user)) {str = "SUCCESS: The Department manager agrees with" + user + "" "The meal fee, the amount is" + fee + "Yuan"; }else {//Others do not agree with str = "Failure: Department manager does not agree with" + user + "" The cost of the meal, the amount is "+ fee +" Yuan "; }}else {//More than 1000, continues to pass to higher-level people handling if (getsuccessor ()! = null) { Return Getsuccessor (). Handlefeerequest (user, fee); }} return str; }}
public class Generalmanager extends Handler { @Override public string handlefeerequest (string user, double fee) { String str = ""; The general manager's authority is very large, as long as the request to here, he can handle if (fee >=) { //In order to test, simple point, only agree to Zhang San request if ("Zhang San". Equals (user) { str = "SUCCESS: The general manager agrees" "+ user +" "The cost of the meal, the amount is" + fee + "Yuan"; } else { //Everyone else does not agree with str = "Failure: The general manager does not agree" + user + "" The meal fee, the amount is "+ fee +" Yuan "; } } else { //If there are subsequent processing objects, continue to pass if (getsuccessor ()! = null) { return getsuccessor (). Handlefeerequest (user, fee); } } return str; }}
Client Class
public class Client {public static void main (string[] args) {//First to assemble the responsibility chain Handler h1 = new Generalmanager ( ); Handler h2 = new Deptmanager (); Handler h3 = new Projectmanager (); H3.setsuccessor (H2); H2.setsuccessor (H1); Start Test String test1 = H3.handlefeerequest ("Zhang San", 300); System.out.println ("test1 =" + test1); String test2 = h3.handlefeerequest ("John Doe", 300); System.out.println ("test2 =" + test2); System.out.println ("---------------------------------------"); String test3 = H3.handlefeerequest ("Zhang San", 700); System.out.println ("test3 =" + test3); String test4 = h3.handlefeerequest ("John Doe", 700); System.out.println ("test4 =" + test4); System.out.println ("---------------------------------------"); String test5 = H3.handlefeerequest ("Zhang San", 1500); System.out.println ("test5 =" + test5); String Test6 = h3.handlefeerequest ("John Doe", 1500); System.out.println ("Test6 =" + Test6); }}
where is the responsibility chain flexible?1. Change the internal delivery rulesinternally, the project manager can completely skip the personnel department to that level and find the general manager directly. Everyone can dynamically designate his successor. 2. You can start at any level of the responsibility chain. If the project manager is not in, you can go directly to the department manager, the responsibility chain will continue, no impact. 3. The difference between using and notwithout the structure of the chain of responsibility, we need to have a coupling relationship with every level in the company. if it is reflected in the code, even if we need to write a lot of ugly if....else statements in a class. If we use the chain of responsibility, we are dealing with a black box, we just need to know one of the departments, and then let the inside of the black box to be responsible for the transfer.
Pure and impure chain of Duty modea pure chain of responsibility requires a specific handler object to choose only one of two behaviors: one is to take responsibility, but to push the responsibility to the next. A situation in which a specific processor object is not allowed to transmit responsibility after taking part of the responsibility. in a pure chain of responsibility, a request must be received by a handler object, and in an impure chain of responsibility, a request can eventually not be received by any of the receiving end objects. The actual example of the pure responsibility chain model is difficult to find, and the examples we see are the implementation of the impure responsibility chain model.
More Design Patterns:23 design Mode series
jason0539
Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)
Recommended sweep code pay attention to the public number, to add color to life
The model of responsibility chain and responsibility chain in Java design mode