Step by step. NET Design Mode Study Notes 19. Chain of Responsibility (Responsibility Chain mode)

Source: Internet
Author: User

Overview

During the software build process, a request may be processed by multiple objects, but each request can only have one receiver at runtime, it will inevitably bring about a tight coupling between the request sender and the receiver.
How does one make the request sender not need to specify a specific receiver? Let the request recipient decide to process the request at runtime, so as to decouple the two.

Intention

The responsibility chain mode is an object behavior mode [GOF95 ]. 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 the chain and assign responsibility without affecting the client.

Structure chart

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 an abstract class or interface.

ConcreteHandler role: after receiving a request, the handler can choose to process the request or send the request to the next home. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.

 

Examples in life

Drumming is a lively and intense drinking game. At the banquet, guests are arranged in sequence to drum up one person. The drums are separated from the flowers 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.

It is the application of the responsibility chain model. A responsibility chain may be a straight line, a link chain, or a part of a tree structure.

 

Example

In the company, employees have the opportunity to go on a business trip. When a business trip is made, a loan will be generated. when the loan is made, the question of who will approve the application form will be generated, department managers, directors, and Chairman of the Board have different approval permissions. The following figure shows the responsibility chain model we designed based on this requirement:

 

Code Design

Create BorrowApplication. cs First:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_981322 "> view sourceprint?
01 public class BorrowApplication
02 {
03     private string _Name;
04     private double _Money;
05     private string _Purpose;
06  
07     public string Name
08     {
09         get { return _Name; }
10         set { _Name = value; }
11     }
12  
13     public double Money
14     {
15         get { return _Money; }
16         <

Related Article

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.