Design Model (12) responsibility chain model
I. drumming and spreading flowers
Drumming is a lively and intense drinking game. At the banquet, the guests are seated at a time. One person is playing the drums, and the place where the drums are played is separated from the place where the flowers are transmitted to show justice. When the drum is started, the bouquet begins to pass in sequence, and the sound of a drum falls. If the bouquet is in the hands of someone, the person will have to drink.
For example, Jia Mu, Jia Yi, Jia Zheng, Jia Baoyu, and Jia Huan are the five painters who participate in the drum and flower games. They form a chain link. The tambourine passes the flowers to Jia mu to start the flower games. The flowers are sent from Jia mu to Jia, and Jia Zheng to Jia Baoyu. Jia Baoyu is sent to Jia Huan, and Jia Huan is sent to Jia Mu, as shown in.
It is a typical mode of responsibility chain.
2. What is the responsibility chain model?
The responsibility chain mode is an object behavior mode. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.
Iii. Structure of the responsibility chain model
● Abstract Handler role: defines 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 aggregate relationship of the Handler class in gives reference to the sub-class. The abstract method handleRequest () standardizes the operations of the sub-class to process requests.
● ConcreteHandler role: after receiving a request, the handler can choose to process the request or send the request to the next user. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.
Iv. Instances of the responsibility chain model
To better understand the structure of the responsibility chain model, let's take a look at the simplest code implementation.
Package com. designpattern. pre1;/*** abstract Handler, define the Handler interface ** @ author 98583 **/public abstract class Handler {/*** next Handler */protected Handler successor; /*** processing method for each Handler */public abstract void handleRequest ();/*** set the next Handler ** @ param successor */public void setSuccessor (Handler successor) {this. successor = successor;}/*** get the next Handler ** @ return */public Handler getSuccessor () {return successor ;}}
Package com. designpattern. pre1;/*** specific processor ** @ author 98583 **/public class ConcreteHandler extends Handler {/*** Processing Method */public void handleRequest () {/*** if there is one handler, it will be handed over to the next handler. However, in actual situations, it should be determined whether the handler can handle the problem, it cannot be processed before being passed to the next processor */if (getSuccessor ()! = Null) {System. out. println (The request is passed to + getSuccessor (); getSuccessor (). handleRequest ();} // process else {System. out. println (The request is handled here .);}}}
Package com. designpattern. pre1; public class Client {static private Handler handler1, handler2; public static void main (String [] args) {/*** defines two processors */handler1 = new ConcreteHandler (); handler2 = new ConcreteHandler (); handler1.setSuccessor (handler2); handler1.handleRequest ();}}
Now that we understand the basic structure of the responsibility chain model, we can implement the story of drumming and passing flowers in the Dream of Red Mansions above.
Package com. designpattern. chainofresp;/*** equivalent to abstract Handler * @ author 98583 **/abstract class Player {abstract public void handle (int I ); /*** next processor */private Player successor; public Player () {successor = null ;} /*** set the next processor * @ param aSuccessor */protected void setSuccessor (Player aSuccessor) {successor = aSuccessor;}/*** to the next processor, this method should not appear here, because each handler has this method, so it is put into the parent class * @ param index * /Public void next (int index) {if (successor! = Null) {successor. handle (index);} else {System. out. println (Program terminated .);}}}
Package com. designpattern. chainofresp;/*** the class of Jia Mu is equivalent to ConcreteHandler * @ author 98583 **/class JiaMu extends Player {public JiaMu (Player aSuccessor) {this. setSuccessor (aSuccessor);}/*** processing method for this and */public void handle (int I) {if (I = 1) {System. out. println (Jia Mu gotta drink !); } Else {System. out. println (Jia Mu passed !); /*** Pass to the next processor */next (I );}}}
The classes of others are similar to those of Jia Mu, so they are not pasted, and the source code is attached.
Package com. designpattern. chainofresp;/*** specifies the target user, which is equivalent to the client * @ author 98583 **/public class DrumBeater {private static Player player Player; static public void main (String [] args) {JiaMu jiaMu = new JiaMu (null); jiaMu. setSuccessor (new JiaShe (new JiaZheng (new JiaBaoYu (new JiaHuan (jiaMu); player = jiaMu; player. handle (4 );}}
V. Responsibility Chain Model
Advantages:
Reducing coupling can simplify object connection and enhance the flexibility of assigning responsibilities to objects. It is convenient to add new request processing classes.
Disadvantages:
There is no guarantee that the request will be affected by the performance of the receiving system, and it is not convenient for code debugging. It may cause loop calls.
Vi. Applicable Environment
1. Multiple objects can process the same request. The runtime determines which object will process the request automatically.
2. If the receiver is not explicitly specified, submit a request to one of multiple objects.
3. A group of objects can be dynamically specified to process requests.