In this pattern, the request is sent to the first handler, if the request can be processed by that, it will return the response, or it will be passed to its successor for processing, until it gets to the appropriate handler who could process the request.
To demonstrate the fact let us take an example from the above class diagram.
public interface IHandler
{
IHandler Successor { get; set; }
void ProcessRequest(IRequest request);
}
public class FirstRequestHandler : IHandler
{
public IHandler Successor
{
get;
set;
}
public void ProcessRequest(IRequest request)
{
Console.WriteLine("Request Received by FirstRequesthandler for processing", request);
if (request.Seed <= 1)
{
// Process request
}
else
{
this.Successor.ProcessRequest(request);
}
}
}
public class SecondRequestHandler : IHandler
{
public IHandler Successor
{
get;
set;
}
public void ProcessRequest(IRequest request)
&nbode style="color:#000000;">{
get;
set;
{
Console.WriteLine("Request Received by SecondRequestHandler for processing", request);
if (request.Seed < 5)
{
// Process request
}
else
{
this.Successor.ProcessRequest(request);
}
}
}