24-day design model ------ responsibility chain model, 24-day Design Model
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka/article/details/43210027
1. Chain of Responsibility Pattern 1. Introduction
You can probably guess the general appearance of this pattern from the name-there will be multiple objects with similar processing capabilities in the system. When a request is triggered, the request will be passed in the chain composed of these objects until the most appropriate "responsibility" object is found and processed. The Design Pattern defines it as follows: Multiple objects have the opportunity to process requests, so as to avoid coupling between request senders and receivers. Connect these objects into a chain and pass the request along the chain until an object processes it. From the definition, we can see that the responsibility chain model is proposed to "decouple" the system to adapt to the changes and ambiguity of system requirements.
2. Intention
So that multiple objects have the opportunity to process the request, so as to avoid coupling between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it.
3. Applicability there are multiple objects that can process a request, and the object that processes the request is automatically determined at the runtime. Else you want to submit a request to one of multiple objects without specifying the receiver explicitly. Bytes
The object set that can process a request should be dynamically specified.
4. Structure
The roles involved in the responsibility chain mode are as follows:
●Abstract Handler role:Define an interface for processing requests. If necessary, the interface can define a method to set and return references to the next house. This role is usually implemented by a Java Abstract class or Java interface.
●The specific handler (ConcreteHandler) role:After receiving the request, the specific handler can choose to process the request or send the request to the next house. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.
Ii. Example
Here is a question about wage adding. The problem of wage adding with different quotas should be solved by different levels of leaders. HR (unable to solve the salary problem, it will only report to the Board)-"shift leader-" Manager-"Chairman
Categories requiring higher salaries
/*** File name: RaiseSalary. java * Description: Responsible Chain Mode * created by: Lin bingwen * Date: 2015.1.27 **/package RaiseSalary; /** raise the salary class **/public class RaiseSalary {private int num; // The required salary. // The constructor, the required salary. public RaiseSalary (int num) {this. num = num;} // get the expected salary public int GetNum () {return num;} public String toString () {return "[requirement for higher salary" + num + "RMB problem]" ;}}
Abstract class for solving wage Problems
/*** File name: Support. java * Description: Responsible Chain Mode * created by: Lin bingwen * Date: 2015.1.27 **/package Support; import RaiseSalary. *;/** abstract class to solve the problem of raising the salary **/public abstract class Support {private String name; // solve the problem of raising the salary by name private Support next; // The problem is solved by the next person. // The solution for raising the salary is public Support (String name) {this. name = name;} // set the transfer location public Support SetNext (Support next) {this. next = next; return next;} // public final void GetTrobule (RaiseSalary raisesalary ) {If (Resolver (raisesalary) {succmcm (raisesalary);} else if (next! = Null) {next. getTrobule (raisesalary);} else {Fail (raisesalary) ;}// print the public String toString () {return "[" + name + "]";} // Solution to the Problem protected abstract boolean Resolver (RaiseSalary raisesalary); // solve the problem, and get the salary added with protected void succmcm (RaiseSalary raisesalary) {System. out. println (raisesalary + "solved by" + this +. ");} // The problem cannot be solved. protected void Fail (RaiseSalary raisesalary) {System. out. println (raisesalary +" ");}}
Then there are various usage
/*** File name: Main. java * Description: Responsible link mode * created by: Lin bingwen * Date: 2015.1.27 **/package Main; import Support. *; import RaiseSalary. *;/** HR can only collect issues requiring higher salary and report them to **/class HR extends Support {public HR (String name) {super (name );} // HR cannot solve the problem requiring higher salaries @ Overrideprotected boolean Resolver (RaiseSalary raisesalary) {// return false, the method stub automatically generated by TODO ;}} /** the leader can handle **/class ZuZhang extends Support {private int limit; // It can increase the maximum salary. // constructor, public ZuZhang (String name, int limit) {super (name); this. limit = limit;} @ Overrideprotected boolean Resolver (RaiseSalary raisesalary) {if (raisesalary. getNum () <= limit) {return true;} else {return false ;}}/ ** the manager can handle the problem **/class JingLi extends Support {private int limit; // raise the maximum wage limit // constructor. The manager can solve the problem of requiring higher salaries. public JingLi (String name, int limit) {super (name); this. limit = limit;} @ Overrideprotected boolean Resolver (RaiseSalary raisesalary) {if (raisesalary. getNum () <= limit) {return true;} else {return false ;}}/ ** the Chairman can handle the problem **/class DongShiZhang extends Support {private int limit; // raise the maximum wage limit // constructor, the Chairman can handle the question of the number of wage increases required public DongShiZhang (String name, int limit) {super (name); this. limit = limit;} @ Overrideprotected boolean Resolver (RaiseSalary raisesalary) {if (raisesalary. getNum () <= limit) {return true;} else {return false ;}} public class Main {public static void main (String [] args) {Support evan = new HR ("HR Evan"); Support kaka = new ZuZhang ("team lead kaka", 100 ); // Support bingbing = new JingLi ("Manager bingbing", 100 ); // The manager can solve the problem of raising the salary by up to 500. Support wenwen = new DongShiZhang ("Chairman wenwen", 1000 ); // The manager can solve the problem of raising the salary by 500 at most // constitute a chain of responsibility evan. setNext (kaka ). setNext (bingbing ). setNext (wenwen); // the problem that the employee is expected to raise his salary. for (int I = 50; I <1200; I ++ = 50) {evan. getTrobule (new RaiseSalary (I ));}}}
Result output:
[The problem of raising the salary by 50 yuan] was solved by [leader kaka.
[The problem of raising the salary by 100 yuan] was solved by [leader kaka.
[The problem of raising the salary by 150 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 200 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 250 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 300 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 350 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 400 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 450 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 500 yuan] was solved by [Manager bingbing.
[The problem of raising the salary by 550 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 600 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 650 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 700 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 750 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 800 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 850 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 900 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 950 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 1000 yuan] was solved by [Chairman wenwen.
[The problem of raising the salary by 1050 yuan] cannot be solved
[The problem of raising the salary by 1100 yuan] cannot be solved
[The problem of raising the salary by 1150 yuan] cannot be solved
Iii. Advantages and Disadvantages 1. Advantages:
(1) Reduce coupling. It decouples the request sender and receiver.
(2) simplified the object. So that the object does not need to know the structure of the chain.
(3) improve the flexibility of assigning responsibilities to objects. By changing members in the chain or mobilizing their order, you can dynamically add or delete responsibilities.
(4) It is convenient to add a new request processing class.
2. Disadvantages:
(1) the request cannot be guaranteed to be received.
(2) the system performance will be affected, and it is not convenient to debug the Code; it may cause loop calls.
(3) It may not be easy to observe the features during running, which may affect debugging.
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka/article/details/43210027