Exercise: Chain of Responsibility
This exercise puts the Chain of Responsibility pattern into code.
Scenario
A purchase-approval system routes each request up a chain of authority. Three handlers sit in a fixed order: Team Lead, Manager, and Director. Each handler has an approval limit. When a request arrives, the first handler whose limit covers the amount approves it; if no handler can approve the amount, the chain rejects it.
Approval limits:
| Role | Limit |
|---|---|
| Team Lead | up to 100 |
| Manager | up to 1,000 |
| Director | up to 10,000 |
Commands
| Command | Behavior | Output |
|---|---|---|
["request", amount] | Route the amount up the chain; the first handler whose limit covers it approves it | "<Role> approved <amount>" or "Rejected: <amount> exceeds all limits" |
The program reads one command per line and prints one line per command.
5 request 50 request 500 request 5000 request 100 request 50000
Team Lead approved 50 Manager approved 500 Director approved 5000 Team Lead approved 100 Rejected: 50000 exceeds all limits
Your task
Create TeamLead, Manager, and Director — each a subclass of Handler — so the
commands produce the output above. The starter keeps the Handler base class (with
the next link and the forwarding logic), the chain-wiring function, and the
run_approvals dispatcher. A TODO comment in the starter lists each class's expected
role name and approval limit.