This review studies the responsibility chain mode of the Zen of design pattern.
What is the responsibility chain model?
Avoid coupling the sender of a request to their receiver by giving + than one object a chance to handle the request. Chain the receiving objects and pass the request along the Chain until an object handles it. (To avoid coupling between the requester and the responder, each object has an opportunity to process the request.) Link the requested object to a chain and pass the request along the chain until an object is able to handle the request)
For example, the lost and found office issued a claim for the lost property, and then a B-butyl to claim, the methyl ethyl-propyl string into a chain of responsibility, a first to see if his items, not to B, and then see if it is their own, not passed to the third, and so on, until the lost property was claimed. If...else If....else If....else is actually the small embodiment of the responsibility chain model.
What are the benefits of using the responsibility chain model?
As the definition says, separate the request from the processing, and the requester can not know who is dealing with it, and the processor may not need to know who requested it. Compared with If...else If....else, the flexibility is higher, the coupling degree is lower and the readability is higher.
What are the drawbacks of the responsibility chain model?
As with If...else, the chain of responsibility is to traverse the list before finding the request handler, and if the chain of responsibility is long, the efficiency will be low.
When do I use the responsibility chain model?
(1) When there are many if in the code: Else statement, and can seriously affect readability, consider refactoring to the responsibility chain pattern
(2) If the code needs to add a new class of processing requests high probability and frequent, can consider the responsibility chain model, the responsibility chain model flexibility is high
How do I use the responsibility chain model?
UML Class Diagrams:
1, define a processing request handler class, can be an abstract class, can also be an interface, generally there are two methods (interfaces), one is Setnext (), used to specify the responsibility chain of the next node, one is handle (), to implement the processing of the request.
2, defines the Concretehandler class, inherits the handler abstract class (implements the handler interface), and implements the Setnext () and the handle () method.
3. Define the Scene class (Client), set the order of Concretehandler, and call the handle () method of the first Concretehandler.
For example, enter two numbers, give a command (ADD/SUB/MULT/DIV), perform a subtraction operation, and implement the following with the responsibility chain pattern:
UML diagram
Public interface Chain {
void Setnextchain (Chain nextchain); void calculate (Numbers request);}
Numbers class
public class Numbers { private int number1; private int number2; Private String calculatewanted; Public Numbers (int number1, int number2, String calculatewanted) { this.number1 = number1; This.number2 = number2; this.calculatewanted = calculatewanted; } public int GetNumber1 () { return number1; } public int GetNumber2 () { return number2; } Public String getcalculatewanted () { return calculatewanted; }}
AddNumbers class:
public class AddNumbers implements Chain { private Chain nextchain; @Override public void Setnextchain (Chain nextchain) { this.nextchain = Nextchain; } @Override public Void Calculate (Numbers request) { if (request.getcalculatewanted (). Equals ("Add")) { System.out.println (Request.getnumber1 () + "+" + request.getnumber2 () + "=" (Request.getnumber1 () + Request.getnumber2 () )); } else { nextchain.calculate (request);}}}
Subnumbers, Multnumbers, divnumbers also pain addnumbers structure, no longer write, just us divnumbers as the responsibility chain tail, it has no successor node, so in the divnumbers in the Else statement output "Works for Add, Sub, mult, div" only.
Client class:
public class Client {public static void Main (string[] args) { Chain ChainCalc1 = new AddNumbers (); Chain CHAINCALC2 = new Subnumbers (); Chain chainCalc3 = new Multnumbers (); Chain CHAINCALC4 = new Divnumbers (); Chaincalc1.setnextchain (CHAINCALC2); Chaincalc2.setnextchain (CHAINCALC3); Chaincalc3.setnextchain (CHAINCALC4); Numbers request = new Numbers (4, 2, "add"); Chaincalc1.calculate (request); } }
Run Result: 4 + 2 = 6
What are the considerations for using the responsibility chain model? The number of nodes in the chain of responsibility to control, avoid the super-long chain impact performance and debugging, the general practice is to set a maximum number of nodes in the handler, in the Setnext to determine whether the threshold is exceeded, the chain is not allowed to build, but I think the number of human control nodes is better, After all, most of the time, the chain will not be long, each traversal to determine whether it is long, some waste.
Responsibility chain model (Chain of Responsibility)