Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/40018231
Responsibility chain Mode (chainofresponsibility): There are multiple objects, each holding a reference to the next object, forming a chain that requests to pass through the chain until an object decides to process the request, but the issuer is not sure which object will eventually process the request.
First, UML modeling:
Second, the Code implementation
/** * Responsibility Chain mode: There are multiple objects, each holding a reference to the next object, forming a chain, * * Request passed on this chain until an object decides to process the request, * but the issuer does not know which object will eventually process the request. */interface Handler {public void operator ();} /** * Handler are individually encapsulated here, making it easy to modify reference objects */abstract class Abstracthandler implements Handler {private Handler handler;public Handl Er gethandler () {return handler;} public void SetHandler (Handler Handler) {this.handler = Handler;}} Class MyHandler extends Abstracthandler implements Handler {private string name;public MyHandler (String name) {this.name = Name;} @Overridepublic void operator () {if (GetHandler ()! = null) {System.out.print (name + "," Give the Bug ");/** * Here is the key. "Note 1" Here is not recursive oh ~ * * Recursion: A (operator)-->a (operator)-->a (operator) * * Liability chain: A (operator)-->b (operator)-->c ( operator) */gethandler (). operator ();} else {System.out.println (name + "process bug...\n");}}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {MyHandler handler1 = new Myhand Ler ("technical director"); MyHandler handler2 = new MyHandler ("Project manager"); MyHandler Handler3 = new MyHandler ("programmer");/** * If you do not have the next, you will process * * Printing results: Technical Director to deal with the bug ... */handler1.operator ();/** * As long as there is a place, it will be passed to the bottom of the processing * * The following print results: Technical director, handing the bug to the project manager, handing the bug to the programmer to deal with the bug ... * in this way, the original technical director to handle the bug, now a layer of responsibility to the programmer to deal with the */handler1.sethandler ( HANDLER2); Handler2.sethandler (HANDLER3);/** * Through the printing results can be known: MyHandler instantiation will generate a series of mutually held objects (handler), forming a chain. */handler1.operator ();/** * "Note 2" The chain of responsibility is not linked list: The list has a head node, we have to go through the header node each time to access the following nodes * * and the chain of responsibility it can be accessed from the beginning, or can start from among the access, such as: Handler2.operator (); */}}
Iii. Summary
1, the responsibility chain model can be achieved, in the concealment of the client (who does not know the specific treatment of the person who is), the system is dynamically adjusted.
2, the link on the request can be a chain, can be a tree, can also be a ring, the pattern itself does not constrain this, need to implement themselves, at the same time, in a moment, the command allows only one object to be passed to another object, and not allowed to pass to multiple objects.
Java Design Patterns Rookie series (18) modeling and realization of responsibility chain model