Part 3 of 6 · 2 min read
Classes
A bank behind an interface, and a dispenser that plans first.
| Class | Kind | Job in one sentence |
|---|---|---|
Account |
entity | Holds a balance and today's total, and guards debit, credit and undo |
Bank |
interface | Checks a PIN, says whether a card is blocked, and returns the account |
SimpleBank |
class | An in-memory bank for the exercise, counting wrong PINs |
Dispenser |
interface | Plans a payout for an amount, and dispenses a plan |
NoteDispenser |
class | Holds the note stock and searches for a payout |
Atm |
entity | Holds the session mode and runs each customer action in the right order |
Mode |
enum | Idle, card in, signed in |
Choices to say aloud:
- The
Bankis an interface, since the real bank is a remote system that the machine does not own. A test uses an in-memory bank (Level 5, module 5). - PIN checking and the count of wrong tries belong to the bank, since a stolen card could be tried at many machines, and the machine would not see the total. The machine keeps its own count of tries only to end the session.
- The
Dispenserhas two steps,plananddispense. Planning decides which notes make the amount without changing anything. Dispensing does the physical work and updates the stock. Splitting them lets the machine decide before it charges. - The session is a small enum,
Mode, with three values. The vending machine used classes for its states. Here the behaviour per state is only a guard at the top of each method, so an enum is enough. Say that you considered the state pattern and chose the lighter form (Level 5, module 6 on not over-building).
Atm has-one Mode
Atm uses Bank (injected)
Atm uses Dispenser (injected)
Bank has-many Account, one per card
Check yourself: 1. Why does the bank, and not the machine, count wrong PINs? 2. Why does the dispenser have a plan step and a dispense step? 3. Why is an enum enough for the session?