Design Mode-responsibility chain mode
The responsibility chain mode is: Avoid coupling the sender of a request to its own er by giving more than one object a chance to handle the request. chain the processing objects and pass the request along the chain util an object handle it. this means that multiple objects have the opportunity to process the request, thus avoiding the coupling relationship 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.
Two Roles of the responsibility chain model:
Abstract Handler role: defines an interface or abstract class for processing requests, and connects to a successor.
ConcreteHandle: after receiving a request, the role can process the request or pass the request to the next handler.
Class diagram of the responsibility chain model:
Classes corresponding to each role in the responsibility chain mode:
Abstract processor role:
Package com. zz. chain;/*** abstract processor * Copyright May 21, 2015 * created by txxs * all right reserved */public abstract class Handler {private Handler nextHandler; // each processor must process the Request. public final Response handleMessage (request Request) {Response response = null; // determine whether the request is at its own processing level if (this. getHandlerLevel (). equals (request. getRequestLevel () {response = this. echo (request);} else {// does not belong to the processing level of the user. Check whether the next processor if (this. nextHand Ler! = Null) {response = this. nextHandler. handleMessage (request);} else {// no proper Handler, fault tolerance, and self-processing of the business} return response;} // set the public void setNext (handler Handler) of the next handler) {this. nextHandler = handler;} // each handler has a processing Level protected abstract Level getHandlerLevel (); // each handler must implement the processing task protected abstract Response echo (Request request );}
Specific handler role:
Package com. zz. chain;/*** specific processor * Copyright May 21, 2015 * created by txxs * all right reserved */public class ConcreteHandle extends Handler {@ Overrideprotected Level getHandlerLevel () {// TODO Auto-generated method stubreturn null;} @ Overrideprotected Response echo (Request request) {// TODO Auto-generated method stubreturn null ;}}
Advantages of the responsibility chain model:
1. The responsible Chain Mode separates requests from processing. The requestor does not know who handles the requests. The processor does not need to know the full picture of the requests.
2. Improve system flexibility.
Application Mode of the responsibility chain model:
1. A request requires a series of processing work
2. Business Flow Processing