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?
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.
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:
This is the class that generates the request and passes it to the first handler in the chain of responsibility.
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.
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.
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); } }
The classes, interfaces, and objects in the above class diagram can be identified as follows:
Approver- Handler abstract class.
Clerk, Assistant Manager & Manager - ConcreteHandler classes.
Loan & LoanEventArgs - These classes are used for internal processing and hold request details.
// 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(); } }
A set of handlers to handle a request.
A scenario within you needs to pass a request to one handler among a list of handlers at run-time based on certain conditions.
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.