Algo-Sim › Design Patterns
Chain of Responsibility: an ATM
Easy · Design Patterns · step through it in Java, Python and C++ on your own input
Link handlers in a chain; each does its part of a request and passes the rest to the next. The sender only knows the head, and handlers can be added or reordered freely.
Problem
An ATM pays out $50, $20 and $10 notes. Each note size is a handler in a chain: it takes as many of its notes as fit into the amount still owed, then passes the remainder to the next handler. withdraw(amount) returns the total number of notes, or −1 if something is left when the chain ends.
Constraints
- 5 ≤ amount ≤ 500, a multiple of 5
- Each handler is O(1); a withdrawal is O(handlers)
- Adding a $100 handler must not change the ATM class
Edge cases to test
- No handler accepts the request
- A handler that stops the chain early
- An empty chain
Follow-up questions
- Where do you see it in practice?
- HTTP middleware, servlet filters, logging levels and approval workflows.
- How do you avoid a request falling off the end?
- End the chain with a default handler that handles or rejects everything.
Solution
Java
abstract class Dispenser {
private Dispenser next;
Dispenser then(Dispenser next) {
this.next = next;
return next;
}
/** Take what this note can, then pass the rest along the chain. */
int dispense(int amount, int notes) {
int n = amount / note();
amount -= n * note();
if (next != null) return next.dispense(amount, notes + n);
return amount == 0 ? notes + n : -1;
}
abstract int note();
}
class Fifty extends Dispenser { int note() { return 50; } }
class Twenty extends Dispenser { int note() { return 20; } }
class Ten extends Dispenser { int note() { return 10; } }
class ATM {
private final Dispenser chain = new Fifty();
ATM() {
chain.then(new Twenty()).then(new Ten());
}
public int withdraw(int amount) {
return chain.dispense(amount, 0);
}
}Python
class Dispenser:
def __init__(self, note):
self.note = note
self.next = None
def then(self, nxt):
self.next = nxt
return nxt
# Take what this note can, then pass the rest along the chain.
def dispense(self, amount, notes):
n = amount // self.note
amount -= n * self.note
if self.next is not None:
return self.next.dispense(amount, notes + n)
return notes + n if amount == 0 else -1
class ATM:
def __init__(self):
self.chain = Dispenser(50)
self.chain.then(Dispenser(20)).then(Dispenser(10))
def withdraw(self, amount):
return self.chain.dispense(amount, 0)C++
#include <bits/stdc++.h>
using namespace std;
class Dispenser {
unique_ptr<Dispenser> next;
public:
virtual ~Dispenser() = default;
Dispenser &then(unique_ptr<Dispenser> handler) {
next = move(handler);
return *next;
}
virtual int note() const = 0;
int dispense(int amount, int notes) {
int n = amount / note();
amount -= n * note();
if (next)
return next->dispense(amount, notes + n);
return amount == 0 ? notes + n : -1;
}
};
struct Fifty : Dispenser {
int note() const override {
return 50;
}
};
struct Twenty : Dispenser {
int note() const override {
return 20;
}
};
struct Ten : Dispenser {
int note() const override {
return 10;
}
};
class ATM {
Fifty chain;
public:
ATM() {
chain.then(make_unique<Twenty>()).then(make_unique<Ten>());
}
int withdraw(int amount) {
return chain.dispense(amount, 0);
}
};
More Design Patterns problems
- Strategy: swappable discounts (Easy)
- Observer: publish and subscribe (Easy)
- State: a document workflow (Easy)
- Command: undo and redo (Medium)
- Factory: create without naming classes (Easy)
- Singleton: one shared instance (Easy)
- Builder: a pizza step by step (Easy)
- Decorator: stack toppings at runtime (Easy)
- Adapter: two legacy sensors, one interface (Easy)
- Proxy: a caching, lazy price service (Easy)
- Composite: folder sizes (Easy)