Introducing the Chain of Responsibility design pattern

Introducing the Chain of Responsibility design pattern

Read Time : 3 Minutes

Friday, 05 May 2023

Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Chain of Responsibility design pattern.

The Chain of Responsibility pattern is a behavioral design pattern that enables an object to send a request along a chain of handlers until one of them handles it. It is commonly used in systems where multiple objects can handle a single request and the system must determine which object should handle the request dynamically.

The chain of responsibility model consists of the following components:

  • Handler: is an abstract base class that defines the generalities of handling requests.
  • ConcreteHandler: This class is a concrete implementation of the Handler class that handles the requests it is responsible for.
  • Client: is the object that creates the request and sends it to the first handler in the chain.

Here is an example of the Chain of Responsibility pattern in C#:

public abstract class SupportHandler
{
    protected SupportHandler _nextHandler;
    public SupportHandler SetNextHandler(SupportHandler handler)
    {
        _nextHandler = handler;
        return handler;
    }
    public abstract string HandleRequest(int request);
}
public class TechnicalSupportHandler : SupportHandler
{
    public override string HandleRequest(int request)
    {
        if (request <= 100)
        {
            return "Technical support can handle this request.";
        }
        else
        {
            return _nextHandler.HandleRequest(request);
        }
    }
}
public class SalesSupportHandler : SupportHandler
{
    public override string HandleRequest(int request)
    {
        if (request <= 200)
        {
            return "Sales support can handle this request.";
        }
        else
        {
            return _nextHandler.HandleRequest(request);
        }
    }
}
public class GeneralSupportHandler : SupportHandler
{
    public override string HandleRequest(int request)
    {
        return "General support can handle this request.";
    }
}

In this example, the SupportHandler class is an abstract base class that defines a method to handle requests. It also contains a reference to the next handler in the chain.

The TechnicalSupportHandler, SalesSupportHandler, and GeneralSupportHandler classes are specific implementations of the SupportHandler class that handle their own requests.

In the HandleRequest method of each Handler, it checks whether it can perform the request or not. If it can, it handles the request and returns a response. Otherwise, it sends the request to the next Handler in the chain by calling the HandleRequest method of the next controller.

The Chain of Responsibility pattern provides a flexible approach to dispatching requests along a chain of handlers. This pattern is widely used in systems where multiple objects can handle the same request.

If you have used web frameworks such as Asp.Net, Laravel or Express, you must be familiar with middleware. Middlewares are an implementation of the Chain of Responsibility pattern that manage and handle a request entered into the system step by step.

To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.

design patterns

Creational:Factory Design Pattern
Builder Design Pattern
Singleton Design Pattern
Prototype Design Pattern
Abstract Factory Design Pattern
Structural:Adapter Design Pattern
Bridge Design Pattern
Composite Design Pattern
Decorator Design Pattern
Facade Design Pattern
Flyweight Design Pattern
Proxy Design Pattern
Behavioral:Chain of Responsibility Design Pattern
Command Design Pattern
Interpreter Design Pattern
Mediator Design Pattern
Memento Design Pattern
Observer Design Pattern
State Design Pattern
Strategy Design Pattern
Template Method Design Pattern
Visitor Design Pattern
Iterator Design Pattern
  • Share:
reza babakhani
Reza Babakhani

I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.

Latest post

Service Mesh Simplifying Microservice Communication

In the ever-evolving landscape of software development, microservices architecture has gained considerable popularity due to its scalability, flexibility, and extensibility. However, as the number of microservices in an application increases, it becomes increasingly challenging to manage their communication and ensure that they are all properly visible. This is where Service Mesh comes into play.

The Importance of Edge Computing

Due to the speed of technological evolution, one of the concepts that has attracted a lot of attention and changes the way we interact with digital systems is edge computing.

What is event-driven architecture?

Event-driven architecture (EDA) is a software design pattern that has become increasingly popular in modern software development. In this architecture, the flow of data is determined by the occurrence of events. Unlike traditional centralized systems that are constantly checking for new status. Event-driven architecture is especially useful for systems that need to process large amounts of data in real-time.

leave a comment