Responsibility chain Model

Source: Internet
Author: User

1. Definition

Enables multiple objects to have the opportunity to process the request, thereby avoiding the coupling between the sender and the recipient of the request, connecting the objects to a chain, and passing the request along the chain until an object has processed it. The focus of the chain of responsibility is on the "chain", by a chain to handle similar requests in the chain to determine who will handle the request, and return the corresponding results.

2. Code implementation

     Public Abstract classHandler {PrivateHandler _nexthandler;  PublicResponse handlemessage (Request request) {Response Response=NULL; Determine if it is your own level of processingif(Request. Getrequestlevel (). Equals ( This. Gethandlerlevel ())) {Response=Echo (Request); }            Else            {
does not belong to its own processing level, to determine whether there is a next processorif(_nexthandler! =NULL) {Response=_nexthandler.handlemessage (Request); } } returnResponse }
Set the next processor Public voidSetnexthandler (Handler Handler) {_nexthandler=handler; }//Each processor must implement processing tasksprotected AbstractResponse Echo (Request request); Each processor has a processing levelprotected AbstractLevel gethandlerlevel (); } Public classConcretehandler1:handler {/// <summary> ///define your own business logic/// </summary> /// <param name= "Request" ></param> /// <returns></returns> protected OverrideResponse Echo (Request request) {Throw Newnotimplementedexception (); } /// <summary> ///set your own processing level/// </summary> /// <returns></returns> protected OverrideLevel Gethandlerlevel () {Throw Newnotimplementedexception (); } } Public classConcretehandler2:handler {/// <summary> ///define your own business logic/// </summary> /// <param name= "Request" ></param> /// <returns></returns> protected OverrideResponse Echo (Request request) {Throw Newnotimplementedexception (); } /// <summary> ///set your own processing level/// </summary> /// <returns></returns> protected OverrideLevel Gethandlerlevel () {Throw Newnotimplementedexception (); } } Public classConcretehandler3:handler {/// <summary> ///define your own business logic/// </summary> /// <param name= "Request" ></param> /// <returns></returns> protected OverrideResponse Echo (Request request) {Throw Newnotimplementedexception (); } /// <summary> ///set your own processing level/// </summary> /// <returns></returns> protected OverrideLevel Gethandlerlevel () {Throw Newnotimplementedexception (); } }
    /// <summary>    ///request and processing levels/// </summary>     Public classLevel {}/// <summary>    ///Define a request/// </summary>     Public classRequest { PublicLevel Getrequestlevel () {return NULL; }    }    /// <summary>    ///data returned by the processor/// </summary>     Public classResponse {}
    /// <summary>    ///Scene Class/// </summary>    classProgram {Private Static voidMain (string[] args) {
Declares all processing nodes Handler Handler1=NewConcreteHandler1 (); Handler Handler2=NewConcreteHandler2 (); Handler Handler3=NewConcreteHandler3 (); Sets the sequence of stages in the chain handler1. Setnexthandler (Handler2); Handler2. Setnexthandler (HANDLER3); Submit the request and return the result handler1. Handlemessage (NewRequest ()); Console.readkey (); } }

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.