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:
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.
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.
leave a comment