Chain of Responsibility Pattern in Python
Pass a request along a chain of handlers until one of them deals with it, like expense approvals by amount.
- Linking handlers
- Handling or passing on a request
- Changing the chain order
- 1
Approver("Lead", 100)limit 100: pass on - 2
Approver("Manager", 1000)limit 1000: Manager approved 500 - 3
next = Nonewould return "Declined"
Pass it on until someone handles it
The problem
A request may need to be handled by one of several people or rules, depending on its size or type: small expenses by a team lead, larger ones by a manager. Hard-coding who handles what in the sender makes the rules rigid.
Think of it like this
Chain of Responsibility is like an expense claim going up the office. The team lead signs anything up to 100; a bigger claim is handed to the manager, who signs up to 1000. The employee only ever hands the claim to the lead and does not need to know who finally signs it.
Approver("Lead", 100, ...)🧑 The team lead: signs up to 100if amount <= self.limit:✅ Is it within my limit?self.next.approve(amount)⬆️ No: hand it to the next person"Manager approved 500"🧑💼 The manager signs itThe Chain of Responsibility pattern passes a request along a chain of handlers until one of them handles it, so the sender does not need to know which handler will respond.
Key points
- Each
Approverhas alimitand an optionalnextapprover. approvehandles the amount if it is withinself.limit.- Otherwise it calls
self.next.approve(amount), passing the request on. - 500 is above the Lead's limit of 100, so the Manager approves it; above 1000 the result would be
"Declined".
When should you use it?
- Approval workflows with increasing limits.
- Support escalation and request routing.
- Middleware in web frameworks, where each layer can handle or pass on a request.
When not to use it
- Exactly one handler always applies: a lookup is simpler.
- Requests that must be handled by every step: that is a pipeline, not a chain.
Example code
class Approver:
# Chain of Responsibility: offer a request to handlers in order.
def __init__(self, name, limit, next_approver=None):
self.name, self.limit, self.next = name, limit, next_approver
def approve(self, amount):
# Handle locally or pass the request along the chain.
if amount <= self.limit:
return f"{self.name} approved {amount}"
if self.next:
return self.next.approve(amount)
return "Declined"
chain = Approver("Lead", 100, Approver("Manager", 1000))
print(chain.approve(500))
Result of running the code
Manager approved 500
Code explanation
The chain is built by passing the Manager approver as the next approver of the Lead. approve(500) first runs on the Lead, whose limit of 100 is too low, so it forwards the request to self.next. The Manager's limit of 1000 covers 500, so it returns Manager approved 500.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Forgetting the end of the chain
Without a check, the last handler calls None.approve and crashes.
return self.next.approve(amount)
if self.next:
return self.next.approve(amount)
return "Declined"
Finished this lesson?
Mark it as learned to track your progress.