The responsibility chain will probably understand the meaning of this model when you see the name. There are n responsibilities, forming a responsibility chain for pipeline operations.
For example, if you want to ask for leave and ask for leave is a relatively long time (ten months), you can contact the leader team leader. The team lead may be confused by reading the false notes, are you going to have a baby? Ask the project manager. Then you changed to the project manager. The project manager said, "Do you have to take a leave of absence from the National Day on September 11, October? Do you still have to take a ten-month vacation to go home and recuperate. You must tell the project manager that you want to take ten months off. The Project Manager frowned and said, "It's useless. Please ask the boss. When you put the slide in front of the boss, the boss said: Go back, you don't need to come. The execution of this responsibility chain is complete. Of course, if you take a one-day leave, the processing may end at leader. You have no chance to see the project manager and boss.
UML diagram:
It can be seen that all responsibilities are inherited from the same interface. interfaces include methods and the flow of the next responsibility. There is no coupling between responsibilities. You can adjust any order of responsibility, for example, the order of requests can also be the boss-> Project Manager-> the team lead. Although such a responsibility chain should not be generated, but theoretically, it can be implemented.
First, paste our responsibility chain interface:
Public abstract class handle
{
Handle _ HANDLE;
Public handle handlevalue
{
Set {_ HANDLE = value ;}
Get {return _ HANDLE ;}
}
Public abstract string requesthandle (INT level );
}
There are two key points: the handlevalue attribute, which can be used to specify the next responsibility. The requesthandle method is used to process int variables. Of course, you can create more complex objects, such as userinfo. The implementation of specific responsibility classes is as follows:
Public class lowlevel: handle
{
Public override string requesthandle (INT level)
{
If (level <= 8)
{
Return "lowlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class midlevel: handle
{
Public override string requesthandle (INT level)
{
If (level> 8 & level <= 16)
{
Return "midlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class highlevel: handle
{
Public override string requesthandle (INT level)
{
If (level> 16 & level <= 25)
{
Return "highlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class outoflevel: handle
{
Public override string requesthandle (INT level)
{
Return "outoflevel ";
}
}
Note: The final processing method should be added after all processing is completed, for example, outoflevel in this example.
Test code:
Dotapatternlibrary. chainofresponsibility. Handle lobj = new dotapatternlibrary. chainofresponsibility. lowlevel ();
Dotapatternlibrary. chainofresponsibility. Handle mobj = new dotapatternlibrary. chainofresponsibility. midlevel ();
Dotapatternlibrary. chainofresponsibility. Handle hobj = new dotapatternlibrary. chainofresponsibility. highlevel ();
Dotapatternlibrary. chainofresponsibility. Handle oobj = new dotapatternlibrary. chainofresponsibility. outoflevel ();
Lobj. handlevalue = mobj;
Mobj. handlevalue = hobj;
Hobj. handlevalue = oobj;
Landpyform. Form. outputresult (lobj. requesthandle (15 ));
Complete code: Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace dotapatternlibrary. chainofresponsibility
{
Public abstract class handle
{
Handle _ HANDLE;
Public handle handlevalue
{
Set {_ HANDLE = value ;}
Get {return _ HANDLE ;}
}
Public abstract string requesthandle (INT level );
}
Public class lowlevel: handle
{
Public override string requesthandle (INT level)
{
If (level <= 8)
{
Return "lowlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class midlevel: handle
{
Public override string requesthandle (INT level)
{
If (level> 8 & level <= 16)
{
Return "midlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class highlevel: handle
{
Public override string requesthandle (INT level)
{
If (level> 16 & level <= 25)
{
Return "highlevel ";
}
Else
{
Return handlevalue. requesthandle (level );
}
}
}
Public class outoflevel: handle
{
Public override string requesthandle (INT level)
{
Return "outoflevel ";
}
}
}