Please enable Javascript to correctly display the contents on Dot Net Tricks!

Chain of Responsibility Design Pattern - C#

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 44,801   
Support : All C# & .Net
 

Chain of Responsibility pattern falls under Behavioral Design Patterns of Gang of Four (GOF) Design Patterns in .Net. The chain of responsibility pattern is used to process a list or chain of various types of request and each of them may be handle by a different handler. In this article, I would like share what is chain of responsibility pattern and how is it work?

What is Chain of Responsibility Pattern

The chain of responsibility pattern is used to process a list or chain of various types of request and each of them may be handle by a different handler. This pattern decouples sender and receiver of a request based on type of request.

In this pattern, normally each receiver (handler) contains reference to another receiver. If one receiver cannot handle the request then it passes the same to the next receiver and so on.

Chain of Responsibility Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the chain of responsibility design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Client

    This is the class that generates the request and passes it to the first handler in the chain of responsibility.

  2. Handler

    This is the abstract class that contains a member that holds the next handler in the chain and an associated method to set this successor. It also has an abstract method that must be implemented by concrete classes to handle the request or pass it to the next object in the pipeline.

  3. ConcreteHandlerA & ConcreteHandlerB

    These are concrete handlers classes inherited from Handler class. These include the functionality to handle some requests and pass others to the next item in the chain of request.

C# - Implementation Code

public abstract class Handler
{
 protected Handler _successor;
 
 public abstract void HandleRequest(int request);
 
 public void SetSuccessor(Handler successor)
 {
 _successor = successor;
 }
}
 
public class ConcreteHandlerA : Handler
{
 public override void HandleRequest(int request)
 {
 if (request == 1)
 Console.WriteLine("Handled by ConcreteHandlerA");
 else if (_successor != null)
 _successor.HandleRequest(request);
 }
}
 
public class ConcreteHandlerB : Handler
{
 public override void HandleRequest(int request)
 {
 if (request > 10)
 Console.WriteLine("Handled by ConcreteHandlerB");
 else if (_successor != null)
 _successor.HandleRequest(request);
 }
}

Chain of Responsibility Pattern - Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. Approver- Handler abstract class.

  2. Clerk, Assistant Manager & Manager - ConcreteHandler classes.

  3. Loan & LoanEventArgs - These classes are used for internal processing and holds request details.

C# - Sample Code

// Loan event argument holds Loan info
public class LoanEventArgs : EventArgs
{
 internal Loan Loan { get; set; }
}

/// <summary>
/// The 'Handler' abstract class
/// </summary>
abstract class Approver
{
 // Loan event 
 public EventHandler<LoanEventArgs> Loan;

 // Loan event handler
 public abstract void LoanHandler(object sender, LoanEventArgs e);

 // Constructor
 public Approver()
 {
 Loan += LoanHandler;
 }

 public void ProcessRequest(Loan loan)
 {
 OnLoan(new LoanEventArgs { Loan = loan });
 }

 // Invoke the Loan event
 public virtual void OnLoan(LoanEventArgs e)
 {
 if (Loan != null)
 {
 Loan(this, e);
 }
 }

 // Sets or gets the next approver
 public Approver Successor { get; set; }
}

/// <summary>
/// The 'ConcreteHandler' class
/// </summary>
class Clerk : Approver
{
 public override void LoanHandler(object sender, LoanEventArgs e)
 {
 if (e.Loan.Amount < 25000.0)
 {
 Console.WriteLine("{0} approved request# {1}",
 this.GetType().Name, e.Loan.Number);
 }
 else if (Successor != null)
 {
 Successor.LoanHandler(this, e);
 }
 }
}

/// <summary>
/// The 'ConcreteHandler' class
/// </summary>
class AssistantManager : Approver
{
 public override void LoanHandler(object sender, LoanEventArgs e)
 {
 if (e.Loan.Amount < 45000.0)
 {
 Console.WriteLine("{0} approved request# {1}",
 this.GetType().Name, e.Loan.Number);
 }
 else if (Successor != null)
 {
 Successor.LoanHandler(this, e);
 }
 }
}

/// <summary>
/// The 'ConcreteHandler' clas
/// </summary>
class Manager : Approver
{
 public override void LoanHandler(object sender, LoanEventArgs e)
 {
 if (e.Loan.Amount < 100000.0)
 {
 Console.WriteLine("{0} approved request# {1}",
 sender.GetType().Name, e.Loan.Number);
 }
 else if (Successor != null)
 {
 Successor.LoanHandler(this, e);
 }
 else
 {
 Console.WriteLine(
 "Request# {0} requires an executive meeting!",
 e.Loan.Number);
 }
 }
}

/// <summary>
/// Class that holds request details
/// </summary>
class Loan
{
 public double Amount { get; set; }
 public string Purpose { get; set; }
 public int Number { get; set; }
}

/// <summary>
/// ChainOfResponsibility Pattern Demo
/// </summary>
class Program
{
 static void Main(string[] args)
 {
 // Setup Chain of Responsibility
 Approver rohit = new Clerk();
 Approver rahul = new AssistantManager();
 Approver manoj = new Manager();

 rohit.Successor = rahul;
 rahul.Successor = manoj;

 // Generate and process loan requests
 var loan = new Loan { Number = 2034, Amount = 24000.00, Purpose = "Laptop Loan" };
 rohit.ProcessRequest(loan);

 loan = new Loan { Number = 2035, Amount = 42000.10, Purpose = "Bike Loan" };
 rohit.ProcessRequest(loan);

 loan = new Loan { Number = 2036, Amount = 156200.00, Purpose = "House Loan" };
 rohit.ProcessRequest(loan);

 // Wait for user
 Console.ReadKey();
 }
}

Chain of Responsibility Pattern Demo - Output

When to use it?

  1. A set of handlers to handle a request.

  2. A scenario within you need to pass a request to one handler among a list of handlers at run-time based on certain conditions.

  3. Exception handling system in C# is the good example of this pattern. Since an exception thrown by a piece of code in C# is handled by a set of try-catch block. Here catch blocks act as possible handlers to handle the exception.

What do you think?

I hope you will enjoy the Chain of Responsibility Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+