Part 3 of 6 · 1 min read
Classes
Menu, cart, order and listeners.
| Class | Job in one sentence |
|---|---|
MenuItem (record) |
Id, name and price in cents |
Line (record) |
A menu item and a quantity, with a line total |
Restaurant |
Menu and stock. Offers an atomic tryTake and a putBack |
Cart |
A draft: quantities by item id, for one restaurant |
Order |
A placed record with fixed lines and totals, and a state that moves |
OrderState (enum) |
The life cycle |
Listener (interface) |
Called with the order and the old and new states (observer) |
Discount (interface) |
Gives the discount for a subtotal (strategy) |
App |
Places orders, holds the listener list, discount rule and delivery fee |
The records, MenuItem and Line, never change. The Order has a fixed list of lines (List.copyOf) and fixed totals, and only its state moves. This is the immutability habit of Level 5, module 2, with exactly one mutable field, guarded by a lock.
The stock lives in the Restaurant, which makes it the single owner of the truth about how many pizzas are left, just as the seat map was for seats in module 5.
Check yourself: 1. Which classes are records, and why? 2. Which object owns stock? 3. Which two classes are strategies or observers?