Introduction to the Chain of Responsibility Pattern
Introduction to the Chain of Responsibility Pattern
Welcome back! Building on our exploration of various behavioral patterns, we will delve into the Chain of Responsibility Pattern in this lesson. This pattern allows a request to pass through a chain of handlers, giving multiple objects a chance to process the request. It's an excellent way to achieve loose coupling in your code by enabling the request to be processed by different handlers independently.
What You'll Learn
In this lesson, you will learn how to implement the Chain of Responsibility Pattern in Go. This pattern allows you to handle requests flexibly and dynamically. Specifically, you will learn to:
- Create a
HandlerInterface: Define a common interface for all your handlers. - Implement Concrete Handlers: Learn to create specific handlers that deal with different levels of requests.
- Set Up the Chain: Understand how to link these handlers together so that a request passes from one to another until it's handled.
Let's quickly go through an example to understand these concepts:
We start by defining a Request struct and a Handler interface. The Request struct contains the message and the level of the request, while the Handler interface defines the common methods that all handlers must implement:
Next, we create a BaseHandler struct that implements the Handler interface. Notice, the BaseHandler contains a reference to the next handler in the chain by using the next field. The Handle method in the BaseHandler passes the request to the next handler if it exists:
Now, we create specific handlers that extend the BaseHandler and implement the Handle method. In our example, we have FrontDeskHandler, ManagerHandler, and DirectorHandler. Each handler checks if it can handle the request based on the request level. If it can't, it passes the request to the next handler in the chain:
Finally, we set up the chain of handlers and initiate the request by calling the Handle method of the first handler in the chain:
Notice how the request passes through the chain of handlers until it's processed. This is the essence of the Chain of Responsibility Pattern! As you can see, this pattern allows you to handle requests flexibly and dynamically by passing them through a chain of handlers. Therefore, the first request with level 1 is handled by the FrontDeskHandler, the second request with level 2 is handled by the ManagerHandler as front desk can't handle it, and the third request with level 3 is handled by the DirectorHandler as the manager can't handle it.
Let's understand the key components of the Chain of Responsibility Pattern:
- Handler Interface: This interface defines the common methods that all handlers must implement. In our example, the
Handlemethod processes the request, and theSetNextmethod sets the next handler in the chain. - BaseHandler: This is a base class that implements the
Handlerinterface. It contains the logic to pass the request - Concrete Handlers: These are specific handlers that extend the
BaseHandlerand implement theHandlemethod. They decide whether to handle the request or pass it to the next handler in the chain. In our example, we haveFrontDeskHandler,ManagerHandler, andDirectorHandler. - Client Code: This is where you set up the chain of handlers and initiate the request. In our example, we create instances of the handlers, link them together, and pass the request to the first handler in the chain.
Let's now understand the flow of the request in our example:
- The
Requestobject is passed to theFrontDeskHandler. If the request level matches the handler's level, it handles the request; otherwise, it passes the request to the next handler in the chain. - The
ManagerHandlerreceives the request and checks if it can handle it. If not, it passes the request to theDirectorHandler. - The
DirectorHandlerprocesses the request if it matches its level. - If none of the handlers can handle the request, it remains unhandled.
- The client code initiates the request by calling the
Handlemethod of the first handler in the chain.
