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

Chain of Responsibility Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 16 Jul 2013
Total Views : 47,488   
Updated On : 26 Sep 2016
 

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.



Free Interview Books
 
COMMENTS (0)
11 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM-4:30 PM IST

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+