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
A request for 50 is within the Team Lead's limit of 100, so the Team Lead approves it. A request for 500 exceeds the Team Lead's limit but is within the Manager's limit of 1,000, so it passes to the Manager. A request for 5,000 passes through both Team Lead and Manager before the Director (limit 10,000) approves it. The boundary case of exactly 100 is still within the Team Lead's limit and is approved there. A request for 50,000 exceeds all three limits and is rejected.
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.