Defined
An event involves the processing of multiple object sequences, and the passing of a duty function. The object of responsibility is introduced, and the transfer of the uniform interface object between the related duty objects is carried out to avoid the high coupling between initiator and receiver.
In fact, the existence of the responsibility chain is very common, for example, often involved in the approval flow is more classic, leave the approval process is an example: individual application-Manager approval---Understanding of the approval record-general manager approval.
Class diagram
Where handler is the unified base class of the chain of responsibility, inheriting from its derived class as the specific responsible object, when a responsible object processing is completed, the responsible object is pointed to the next.
And Handlerfactory is the responsibility chain of the creation of the factory, because the responsibility chain itself should be set up, rather than the client itself directly to create.
Code
1 Public Abstract classHandler2 {3 PrivateHandler successor =NULL;4 PublicHandler successor5 {6 Get7 {8 return This. Successor;9 Ten } One Set A { - This. successor =value; - } the } - Public Abstract voidHandleRequest (decimalfee); - } - + Public classGrouphandler:handler - { + PublicGrouphandler (Handler Handler) A { at This. successor =handler; - } - Public Override voidHandleRequest (decimalfee) - { - if(Successor! =NULL) - { in successor.handlerequest (fee); - } to } + } - Public classManagerhandler:handler the { * PublicManagerhandler (Handler Handler) $ {Panax Notoginseng This. successor =handler; - } the Public Override voidHandleRequest (decimalfee) + { A if(Fee > the) the { +Console.WriteLine ("Group application fee cannot exceed $3000"); - return; $ } $Console.WriteLine ("approval by manager"); - if(Successor! =NULL) - { the successor.handlerequest (fee); - }Wuyi } the } - Public classBosshandler:handler Wu { - PublicBosshandler (Handler Handler) About { $ This. successor =handler; - } - Public Override voidHandleRequest (decimalfee) - { A if(Fee >2500) + { theConsole.WriteLine ("Group application fee cannot exceed $3000"); - return; $ } theConsole.WriteLine ("approval by general manager"); the if(Successor! =NULL) the { the successor.handlerequest (fee); - } in } the } the About Public classFinacialhnadler:handler the { the PublicFinacialhnadler (Handler Handler) the { + This. successor =handler; - } the Public Override voidHandleRequest (decimalfee)Bayi { theConsole.WriteLine ("Financial release Funds:"+fee. ToString ()); the if(Successor! =NULL) - { - successor.handlerequest (fee); the } the } the } the - Public classhandlerfactory the { the /// <summary> the ///creation of the responsibility chain94 /// </summary> the /// <returns></returns> the Public StaticHandler Createhandler () the {98Handler Finacialhandler =NewFinacialhnadler (NULL); AboutHandler Bosshandler =NewBosshandler (finacialhandler); -Handler Managerhandler =NewManagerhandler (bosshandler);101Handler Grouphandler =NewGrouphandler (managerhandler);102 returnGrouphandler;103 }104 } the 106 Public classClient107 {108 Public voidApplyfee ()109 { theHandlerfactory.createhandler (). HandleRequest (2600);111 } the}View CodeSummarize
The responsibility chain model focuses on the responsibility transfer between the responsible objects, which is different from the command mode and the state mode.
Responsibility chain Model