Golang Pattern: Chain of Responsibility
Created by: Achmad Rizki Nur Fauzie
This tutorial is sponsored by: https://goexperts.tech/
What is a Chain of Responsibility?
Chain of Responsibility is a design pattern (design pattern) used in programming to link a set of objects that have the ability to handle a request or problem sequentially. This pattern helps separate different code according to their function and makes it easier to maintain.
In the Chain of Responsibility pattern, a request or problem is passed from one object to the next in a specified order, until the request is handled or rejected by one of the objects. In this way, every object has a chance to handle the request or problem, and objects that cannot handle the request or problem can be skipped.
When is the right time to apply pattern Chain of responsibility ?
Chain of Responsibility can be applied in a variety of situations, especially when there are a large number of objects that may need to process requests.
Some examples of situations where the Chain of Responsibility can be used are as follows:
- When you have a hierarchy of objects and you want each object to handle a specific request and send that request to the next object in the hierarchy, if the current object cannot handle it.
- When you want to avoid a strong dependency between the request sender and the object handling it.
- When you want to create a system that is flexible and can be modified in the future by adding new objects to the chain without changing the class of the request sender or other objects.
- When you want to limit the number of objects that receive and process requests. In a Chain of Responsibility, each object in the chain has the ability to determine whether they can handle the request or not, and send the request to the next object if they can’t handle it.
Chain of Responsibility is a fairly common design pattern and can be applied to many projects. Following are some examples of projects that use Chain of Responsibility in their implementation:
1. Ethereum Blockchain:
Ethereum blockchain uses the Chain of Responsibility pattern to process transactions. Each block in the blockchain has a different validation function. In Chain of Responsibility, each block contains a reference to the previous block, so transactions can be verified continuously throughout the blockchain.
2. net/http package in Golang:
HTTP request handling in Golang uses the Chain of Responsibility pattern. Each handler has a specific task in processing the request, and the right handler is chosen based on the request path and the HTTP method used
3. Kubernetes:
Kubernetes uses the Chain of Responsibility pattern to manage the objects in a cluster. Each object is broken up into controllers, and each controller is responsible for a specific task in the management of that object. If one controller can’t handle the task, another controller can take over the task.
Lets Code in Golang
We define a Request struct that will be used as an object to be processed by each handler. Next, we define the Handler interface which has two methods: SetNext to define the next handler in the chain and Handle to process requests. The Handle method returns a boolean value indicating whether the request has been processed.
The code above defines a type called ‘BankHandler’ that implements the Handler interface. The ‘BankHandler’ has a single field next of type Handler, which represents the next handler in the chain.
The SetNext method of ‘BankHandler’ takes a handler as an argument and sets it as the next handler in the chain. It then returns the handler that was passed in as an argument.
The Handle method of ‘BankHandler’ takes a pointer to a Request struct as an argument. If the amount of the request is less than 1000, it processes the request and returns true.
If the amount of the request is greater than or equal to 1000, it checks whether the next handler is set. If it is, it calls the Handle method of the next handler in the chain and returns its result. If the next handler is not set, it returns false.
Lets Finish the code:
Thats all for today
See you in the next article