Chain of Responsibility design pattern can be used to
process multiple types of request and each type of request can be handled by
multiple handlers. Chain of Responsibility design pattern falls under behavioral pattern of
GOF (Gang of Four) pattern.
When to use –
Chain of Responsibility pattern can be used when multiple types
of request are handled by multiple handlers. If one handler is not able to
process your request, then it will pass to another handler. In this pattern,
each handler has reference to next handler. If one handler can’t process request,
then it passes it to next handler.
Major components of Chain of Responsibility pattern –
Handler – This is an abstract class. This holds
reference of next handler and also has abstract method to handler request.
Concrete Handler – This is concrete class which inherits
from Handler class and implements abstract method to handle request.
Client – This is client class and create requests and
passes to different handlers in the chain of responsibility pattern.
See below example of Chain of responsibility pattern.
Code –
//Handler class
public abstract class Approver
{
public abstract void
HandleRequest(int requestAmount);
protected Approver
NextApprover;
public void
SetNextApprover(Approver approver)
{
NextApprover = approver;
}
}
//Concrete Handler
class
public class Manager : Approver
{
public override void
HandleRequest(int requestAmount)
{
if (requestAmount < 100000)
Console.WriteLine("Manager
has approved amount - {0}", requestAmount);
else if
(NextApprover != null)
NextApprover.HandleRequest(requestAmount);
}
}
//Concrete Handler
class
public class SeniorManager : Approver
{
public override void
HandleRequest(int requestAmount)
{
if (requestAmount < 200000)
Console.WriteLine("Senior
Manager has approved amount - {0}", requestAmount);
else if
(NextApprover != null)
NextApprover.HandleRequest(requestAmount);
}
}
//Concrete Handler
class
public class Director : Approver
{
public override void
HandleRequest(int requestAmount)
{
if (requestAmount < 300000)
Console.WriteLine("Director
has approved amount - {0}", requestAmount);
else if
(NextApprover != null)
NextApprover.HandleRequest(requestAmount);
}
}
//Client Class
class Program
{
static void Main(string[] args)
{
//Create classes of chain of
responsibility
Approver Manager
= new Manager();
Approver
SeniorManager = new SeniorManager();
Approver
Director = new Director();
//Set next request approver or
handler
Manager.SetNextApprover(SeniorManager);
SeniorManager.SetNextApprover(Director);
//Create
requests.
Manager.HandleRequest(75000);
Manager.HandleRequest(150000);
Manager.HandleRequest(250000);
Console.ReadLine();
}
}
Output –
As you can see in above example, Approver abstract class is
inherited to respective approvers. In client class, instance of each
concrete handler class created as handler class and set its next approver or handler for
each concrete handler class. At last manager class handle request method called all
the times. If manager class not able to process request it will pass
on to its next approver and so on in chain of responsibilities classes.
I hope this article helps you to know more about Chain of
Responsibility pattern. Please leave your feedback in comments section below.
References –
See Also –
Creational Patterns
|
Structural Patterns
|
Behavioral Patterns
|
|
||
|
|
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment