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 |
04 |
private double _Money; |
05 |
private string _Purpose; |
10 |
set { _Name = value; } |
15 |
get { return _Money; } |