Chain of Responsibility – Python Design Patterns
Chain of Responsibility method is Behavioral design pattern and it is the object-oriented version of if … elif … elif … else and make us capable to rearrange the condition-action blocks dynamically at the run-time. It allows us to pass the requests along the chain of handlers. The processing is simple, whenever any handler received the request it has two choices either to process it or pass it to the next handler in the chain.
This pattern aims to decouple the senders of a request from its receivers by allowing the request to move through chained receivers until it is handled.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Problem without using Chain of Responsibility Method
Imagine you are building a simple website that takes input strings and tells about the various properties of the strings such as is the string palindrome? is string upperCase? is string lowerCase? and many other properties too. After the complete planning, you decide that these checks for the input string should be performed sequentially. So, here the problem arises for the developer that he/she has to implement such an application that can decide on run-time which action should be performed next.
Solution using Chain of Responsibility Method
Chain of Responsibility Method provides the solution for the above-described problem. It creates a separate Abstract handler which is used to handle the sequential operations which should be performed dynamically. For eg., we create four handlers named as FirstConcreteHandler, SecondConcreteHandler, ThirdConcreteHandler, and Defaulthandler and calls them sequentially from the user class.
This is DefaultHandler telling you that request 'G' has no handler right now. This is FirstConcreteHandler handling request 'e' This is FirstConcreteHandler handling request 'e' This is SecondConcreteHandler handling request 'k' This is ThirdConcreteHandler handling request 's' This is SecondConcreteHandler handling request 'f' This is ThirdConcreteHandler handling request 'o' This is ThirdConcreteHandler handling request 'r' This is DefaultHandler telling you that request 'G' has no handler right now. This is FirstConcreteHandler handling request 'e' This is FirstConcreteHandler handling request 'e' This is SecondConcreteHandler handling request 'k' This is ThirdConcreteHandler handling request 's'
Class Diagram for Chain of Responibility Method
- Single Responsibility Principle: It’s easy to decouple the classes here that invoke operations from classes that perform operations.
- Open/Closed principle: We can introduce the new code classes without breaking th existing client code.
- Increases Flexibility: While giving the responsibilities to the objects, it increases the flexibility of the code.
- Unassured about request: This method doesn’t provide any assurance whether the object will be received or not.
- Spotting characteristics: Due to debugging, it becomes difficult task to observe the characteristics of operations.
- Depriciated System Performance: It might affect the system’s performance due to continuous cycle calls
- Processing several handlers in order: Chain of responsibility method helps very much when it is required to process several handlers in a particular order because the linking is possible in any order
- Decoupling requests: This method is generally used when you want to decouple the request’s sender and receiver.
- Unspecified handlers: When you don’t want to specify handlers in the code, it is always preferred to use the Chain of Responsibility.
Further Read – Chain of Responsibility in Java