Design Mode-responsibility chain mode

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.