Design Mode Study Notes-responsibility chain mode

Source: Internet
Author: User

Overview:

So that multiple objects have the opportunity to process requests, so as to avoid coupling between request senders and recipients. Link these objects into a chain.

And pass the request along this chain until an object processes it.

The idea of this pattern is to give multiple objects the opportunity to process one request, thus decoupling the sender and receiver.

Applicable scenarios:

1. There are multiple objects that can process a request, and the object that processes the request is automatically determined at the runtime.

2. You want to submit a request to one of multiple objects without specifying the recipient explicitly.

3. The object set for processing a request should be dynamically specified.

Class diagram:

Sample Code:

1. abstract interface of responsibility chain

/// <Summary>
/// Define an interface for Request Handling
/// </Summary>
Abstract class Handler
{
Protected handler successor;
/// <Summary>
/// Set the successor
/// </Summary>
/// <Param name = "Successor"> </param>
Public void setsuccessor (handler successor)
{
This. Successor = successor;
}
/// <Summary>
/// Abstract method for processing requests
/// </Summary>
/// <Param name = "request"> </param>
Public abstract void handlerequest (INT request );
}

2. implement various responsibilities on the responsibility chain

/// <Summary>
/// Process the number from 0 to 10
/// </Summary>
Class concretehandler1: Handler
{
Public override void handlerequest (INT request)
{
If (request> = 0 & request <= 10)
{
Console. writeline ("{0} Processing request {1}", this. GetType (). Name, request );
}
Else if (successor! = NULL)
{
// Transfer to the next position
Successor. handlerequest (request );
}
}
}
/// <Summary>
/// Process the number of 10-20 records
/// </Summary>
Class concretehandler2: Handler
{
Public override void handlerequest (INT request)
{
If (request >=11 & request <= 20)
{
Console. writeline ("{0} Processing request {1}", this. GetType (). Name, request );
}
Else if (successor! = NULL)
{
// Transfer to the next position
Successor. handlerequest (request );
}
}
}
/// <Summary>
/// Process the number of 20-30
/// </Summary>
Class concretehandler3: Handler
{
Public override void handlerequest (INT request)
{
If (request >=21 & request <= 30)
{
Console. writeline ("{0} Processing request {1}", this. GetType (). Name, request );
}
Else if (successor! = NULL)
{
// Transfer to the next position
Successor. handlerequest (request );
}
}
}

3. Client call

/// <Summary>
/// Test the responsibility chain mode
/// </Summary>
Static void testchainofresponsibility ()
{
Handler H1 = new concretehandler1 ();
Handler H2 = new concretehandler2 ();
Handler h3 = new concretehandler3 ();
// Set the upstream and downstream of the responsibility chain
H1.setsuccessor (H2 );
H2.setsuccessor (H3 );

Int [] requests = {, 4 };
Foreach (INT request in requests)
{
H1.handlerequest (request );
}
Console. Read ();

}

Summary:

The important idea of the responsibility chain is to define this chain, and this chain has a direction to hide and process changes in requests. The example is also well understood, but the specific application depends on the specific requirements. Using this mode will greatly improve code maintenance and reduce coupling.

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.