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