Chain of Responsibility Design Pattern in C#

Chain of responsibility design pattern comes under behavioral design pattern category of Gang of four (GoF) design patterns. This article explains what is Chain of responsibility pattern and how to implement Chain of responsibility design pattern in C#.

If you want to implement a workflow type of scenario, then chain of responsibility pattern will be very useful. Chain of responsibility design pattern decouples sender and receiver by processing the incoming request with multiple sequential handlers.

Chain of Responsibility Design Pattern

Chain of responsibility design pattern in useful when you want to handle the request by different handlers sequentially (in the chain) before delivering it to the client. This pattern decouples the sender and receiver on the basis of type of request. Observer pattern also decouples the sender and receiver but sequential chain of handlers makes chain of responsibility pattern different.

Since the handlers are present in sequence, so each handler keeps reference to the next handler. Let’s say the request is passed to one handler then first it will try to handle the request and if the handler will not be able to handle the request then it will forward the request to next handler and so on. At last the request is received by receiver or client.

Gang of Four(GoF) Definition:
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Chain of Responsibility Design Pattern – UML Diagram

Chain of responsibility design pattern in C#- UML

Participants in above UML diagram are:

  • IHandler: Defines an interface to handle the requests. All concrete handler class will implement this interface.
  • ConcreteHandler: These classes handle the request of what it is responsible for. If it will not be able to handle the request, then it will forward the request to next handler.
  • Client: This class initiates the request to the first handler present in sequential chain.

Chain of Responsibility Design Pattern in C# – Example

Let’s implement a simple example to understand chain of responsibility design pattern in C#.

Problem Statement

Consider we have to write an application for an organization where employee can apply for the leave. The approval of leave depends upon the days count in applied leave.

  • If leave days are up to 10 working days, then supervisor can approve the leave.
  • If leave days are in between 11 to 30 days, then project manager will approve the leave.
  • If leave days are greater than 30 days, then HR department will approve the leave.

Class Diagram

Chain of responsibility design pattern in C#- ClassDiagram

Participants in above class diagram are:

  • LeaveRequest: request class – this request is handled by different handlers in chain
  • ILeaveRequestHandler: IHandler interface
  • Supervisor, ProjectManager, HR: concrete handler interface

Chain of Responsibility Design Pattern in C# – Implementation Code

Result

Chain of responsibility design pattern in C#- result

Hope you get the idea about how to implement chain of responsibility design pattern in C#. Please post your comments for queries and feedback. Thanks for reading this article.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

2 thoughts on “Chain of Responsibility Design Pattern in C#

  • July 1, 2017 at 3:47 pm
    Permalink

    Hello Shekar

    Your articale is amazing…. but small correction, this Chain of Responsibility belongs to Behaviourl Pattern. IN this article first line printer by mistake.

    Over all your all articales especially examples are amazing.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *