BP4 - Handling Chain - Learn Design Pattern From Simple Things
A complaint travels through support tiers until someone handles it. That's the Chain of Responsibility design pattern explained simply.

Handling Chain (Chain Of Responsibility) is a behavioral design pattern: 🖤🖤🖤🖤🤍
What is Handling Chain?
Handling Chain is a way to set up handlers(GoogleHandler, AmazonHandler, FacebookHandler) in series to find one that can handle the input(a laid-off employee)
Why use Handling Chain?
- It makes the code short and cool to use.
Question: I hate the Handling Chain, can I just use for loop then break?
Answer: Totally agree, If you find this pattern doesn’t make your code better, then ignore it.
When to use Handling Chain?
Question: When do I use Handling Chain?
Answer: It’s like looking for a handler in a list, but the advantage of this technique is that you know that you don’t always ask from the first handler in the series, sometimes you just need to start from the middle.
Input:
- Having handlers(
GoogleHandler,AmazonHandler,FacebookHandler) - Having processing order based on priority (
GoogleHandler > AmazonHandler > FacebookHandler)
# nothing here
Expected Output:
- Input is processed(e.g. Python Dev) by the corresponding handler (Google).
Who wants a Python Dev?
Google: I'll hire this Python Dev
Who wants a Java Dev?
Amazon: I'll hire this Java Dev
Who wants a Golang Dev?
The Golang Dev failed all the interviews.
How to implement Handling Chain?
Non-Handling Chain implementation:
import random
from abc import ABC, abstractmethod
class Handler(ABC):
@abstractmethod
def handle(self, data):
pass
class EAHandler(Handler):
def handle(self, data):
if "Gamer" in data:
return f"EA: I'll hire this {data}"
class GoogleHandler(Handler):
def handle(self, data):
if "Python" in data:
return f"Google: I'll hire this {data}"
class AmazonHandler(Handler):
def handle(self, data):
if "Java" in data:
return f"Amazon: I'll hire this {data}"
class FacebookHandler(Handler):
def handle(self, data):
if random.choice([True, False]):
return f"Facebook: I'll hire this {data}"
if __name__ == "__main__":
ea = EAHandler()
google = GoogleHandler()
amazon = AmazonHandler()
facebook = FacebookHandler()
# build chain
handlers = [ea, google, amazon, facebook]
handlers.remove(ea)
devs = ["Python Dev", "Java Dev", "Golang Dev"]
for dev in devs:
print(f"\nWho wants a {dev}?")
result = None
for handler in handlers:
if _result := handler.handle(dev):
result = _result
break
if result:
print(f" {result}", end="")
else:
print(f" The {dev} failed all the interviews.", end="")
Handling Chain Implementation:
import random
from abc import ABC, abstractmethod
class Handler(ABC):
_next = None
def set_next(self, handler):
self._next = handler
return handler
def pass_to_next(self, data):
return self._next.handle(data) if self._next else None
@abstractmethod
def handle(self, data):
pass
class EAHandler(Handler):
def handle(self, data):
if "Gamer" in data:
return f"EA: I'll hire this {data}"
else:
return self.pass_to_next(data)
class GoogleHandler(Handler):
def handle(self, data):
if "Python" in data:
return f"Google: I'll hire this {data}"
else:
return self.pass_to_next(data)
class AmazonHandler(Handler):
def handle(self, data):
if "Java" in data:
return f"Amazon: I'll hire this {data}"
else:
return self.pass_to_next(data)
class FacebookHandler(Handler):
def handle(self, data):
if random.choice([True, False]):
return f"Facebook: I'll hire this {data}"
else:
return self.pass_to_next(data)
if __name__ == "__main__":
ea = EAHandler()
google = GoogleHandler()
amazon = AmazonHandler()
facebook = FacebookHandler()
# build chain
ea.set_next(google).set_next(amazon).set_next(facebook)
devs = ["Python Dev", "Java Dev", "Golang Dev"]
for dev in devs:
print(f"\nWho wants a {dev}?")
result = google.handle(dev)
if result:
print(f" {result}", end="")
else:
print(f" The {dev} failed all the interviews.", end="")
Related posts
- Software Craft
BP10 - Mediator - Learn Design Pattern From Simple Things
Airport control towers don't let pilots talk directly - they route everything through a hub. That's the Mediator design pattern.
5 min readRead → - Software Craft
BP9 - File History - Learn Design Pattern From Simple Things
Undo and redo look simple on the surface, but how do they work internally? Learn the Memento pattern from simple editing software.
5 min readRead → - Software Craft
BP8 - Visitor - Learn Design Pattern From Simple Things
A doctor examines every patient without the patient changing. That's the Visitor pattern - add operations to objects without modifying them.
6 min readRead → - Software Craft
BP7 - Template Method - Learn Design Pattern From Simple Things
Every barista follows the same coffee recipe but can swap beans and milk. That's the Template Method pattern from simple everyday things.
6 min readRead →