Chain of Responsibility

Chain of Responsibility design 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 to share what is the 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 handled by a different handler. This pattern decouples sender and receiver of a request based on the type of request.

In this pattern, normally each receiver (handler) contains a 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 hold 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 needs 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 a 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.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +