Design Patterns Learning Notes (vi: Responsibility chain model)

Source: Internet
Author: User

Tag: Call change auto to implement STR ISP liability relationship image

1.1 Overview

enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the objects together and pass the request along the chain until an object handles it. This is the responsibility chain model.

The responsibility chain pattern is a mature mode that uses multiple objects to process user requests, the key of which is to assign the user's request to many objects, which are organized into a chain of responsibility, that is, each object contains a reference to the successor, and requires each object on the chain of responsibility to handle the user's request, The user's request is no longer passed to the next object on the chain of responsibility, and if the user's request cannot be processed, the user's request must be passed to the next object on the chain of responsibility.

Structure of the 1.2 pattern

The structure of the responsibility chain model consists of two roles:

(1) Processor (Handler): The processor is an interface that specifies the method by which a specific processor handles user requests, and the method by which a successor object is set by the specific processor.

(2) specific processor (Concretehandler): The specific processor is an instance of the class that implements the processor interface. The specific processor by invoking the processor interface prescribed methods to process the user's request, that is, after receiving the user's request, the processor will invoke the method specified by the interface, in the process of executing the method, if it is found to process the user's request, processing the relevant data, or feedback can not process the information to the user, The user's request is then passed to its successor.

The class diagram for the responsibility chain pattern structure is as follows:

1.3 Responsibility Chain advantages of the model

(1) The object in the chain of responsibility is a low-coupling relationship with its own successor, and is irrelevant to other objects, making it very easy to write processor objects and create chains of responsibility.

(2) When assigning responsibilities in a processor, the chain of responsibility gives the application more flexibility.

(3) Applications can dynamically add, remove, or reassign handlers ' responsibilities.

(4)
Applications can dynamically add, remove, or reassign handlers ' responsibilities.

(5) The application can dynamically change the order of the handlers between them.

(6) Users who use the chain of responsibility do not have to know the processor's information, and the user will not know exactly which object processed its request.

1.4 Fit using the responsibility chain pattern of the scene

(1) There are many objects that can handle a user's request and want the program to automatically determine the object that handles the user while it is running.

(2) If the user does not have to explicitly designate the recipient, a submission request to multiple recipients is required.

(3) The program wants to dynamically develop a collection of objects that can handle user requests.

Design Patterns Learning Notes (vi: Responsibility chain model)

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.