Responsibility chain (Chain of responsibility) mode

Source: Internet
Author: User

Behavioral patterns (behavioral pattern) are abstractions that divide responsibilities and algorithms among different objects. Behavioral patterns are not just about classes and objects, but about the interaction between them.

Behavioral patterns are divided into two types: behavioral patterns of classes and behavior patterns of objects. The behavior pattern of a class: The behavior pattern of a class uses inheritance relationships to assign behavior in several classes. The behavior pattern of an object: The behavior pattern of an object uses the aggregation of the object to distribute the behavior.

The behavioral patterns to be introduced later include the following: Chain of RESP. (Responsibility chain mode) a way of passing a request between a Chain of objects command (Command mode) encapsulate a Command request as an object interpreter (interpreter mode) A way to include language elements in A program iterator (Iteration sub mode) sequentially Access the elements of a collection mediator (intermediary mode) defines simplified communication between classes Memento (Memo mode) Capture and restore an object ' s internal state OBSERVER (Observer mode) a way of notifying change to a number of classes states (status mode) Alter a N object ' s behavior when it state changes strategy (policy mode) encapsulates an algorithm inside a class Template methods (template method mode) D Efer the exact steps of a algorithm to a subclass Visitor (visitor mode) defines a new operation to a class without change

I. Responsibility chain (Chain of responsibility) mode

The responsibility chain pattern is an object's behavior pattern "GOF95". In the mode of responsibility chain, many objects are connected by each object's reference to their house to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client issuing the request does not know which object on the chain ultimately handles the request, allowing the system to dynamically rearrange the chain and assign responsibility without affecting the client.

talking about the flowers from the drums

Drumming is a lively and tense drinking game. In the banquet, the guests sit in order, by a person drumming, drums and the place of the flower is separate, to show just. When the drums started, the bouquets began to pass, the drums fell, and if the bouquet was in someone's hands, the person would have to drink.

It is the application of the chain of responsibility to use drums to spread flowers. The chain of responsibility may be a line, a chain, or part of a tree structure. II. structure of the chain of responsibility model

The roles involved in the responsibility chain pattern are as follows:

Abstract processor (Handler) role: Defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the home. This role is usually implemented by an abstract class or interface.

Specific processor (Concretehandler) role: When a specific processor receives a request, he or she can choose to dispose of the request or pass the request to the other person. Because the specific processor holds the reference to the homes, therefore, if necessary, the specific processor can visit the homes.
Third, the responsibility chain mode of schematic source code
Chain of responsibility pattern--structural example
Using System;

"Handler"
Abstract class Handler
{
Fields
protected Handler successor;

Methods
public void Setsuccessor (Handler successor)
{
This.successor = successor;
}
Abstract public void HandleRequest (int request);
}

"ConcreteHandler1"
Class Concretehandler1:handler
{
Methods
Override public void HandleRequest (int request)
{
if (Request >= 0 && Request < 10)
Console.WriteLine ("{0} handled request {1}",
this, request);
Else
if (successor!= NULL)
Successor. HandleRequest (Request);
}
}

"ConcreteHandler2"
Class Concretehandler2:handler
{
Methods
Override public void HandleRequest (int request)
{
if (Request >= && request < 20)
Console.WriteLine ("{0} handled request {1}",
this, request);
Else
if (successor!= NULL)
Successor. HandleRequest (Request);
}
}

"ConcreteHandler3"
Class Concretehandler3:handler
{
Methods
Override public void HandleRequest (int request)
{
if (Request >= && request < 30)
Console.WriteLine ("{0} handled request {1}",
this, request);
Else
if (successor!= NULL)
Successor. HandleRequest (Request);
}
}

/**////<summary>
Client Test
</summary>
public class Client
{
public static void Main (string[] args)
{
Setup Chain of responsibility
Handler H1 = new ConcreteHandler1 ();
Handler h2 = new ConcreteHandler2 ();
Handler h3 = new ConcreteHandler3 ();
H1. Setsuccessor (H2);
H2. Setsuccessor (H3);

Generate and Process Request
Int[] Requests = {2, 5, 14, 22, 18, 3, 27, 20};

foreach (int request in requests)
H1. HandleRequest (Request);

}
}


Four, pure and impure responsibility chain pattern

A pure responsibility chain pattern requires a specific handler object to select only one of two behaviors: one is to assume responsibility, the other is to push the responsibility to the next. A situation where a specific handler object is not allowed to pass the responsibility down after assuming part of the responsibility.

In a pure chain of responsibility, a request must be received by a handler object, and in an impure chain of responsibility, a request can end up not being received by any receiving end object. The pure responsibility chain pattern example is not easy to find, generally sees the example all is the impure responsibility chain pattern realization. [Dvnews_page]
V. The practical application case of responsibility chain model

The following responsibility chain pattern code demonstrates that people in different positions make decisions about a purchase request or give it to a higher decision-maker based on the permissions set. Chain of responsibility--Real world example
Using System;

"Handler"
Abstract class Approver

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.