Part 3 of 6 · 2 min read
Classes
Nouns become classes, rules find a home.
The nouns in the requirements are vehicle, spot, ticket, lot and fee. Verbs are park, exit, free and charge.
| Class | Kind | Job in one sentence |
|---|---|---|
Vehicle |
value (record) | Holds a plate and a type, and says which spot types it fits |
Spot |
entity | Holds an id, a floor and a type, and whether a vehicle is in it |
Ticket |
value (record) | Records which vehicle took which spot at what time |
FeeStrategy |
interface | Turns a vehicle type and a stay into a fee |
HourlyFee |
class | The fee strategy that charges per started hour |
ParkingLot |
entity | Owns the spots and the open tickets, and enforces the rules |
Clock |
interface | Supplies the current time (Level 5, module 5) |
Where each rule lives follows the rule from Level 0, module 4: the rule goes with the data it needs.
- "Does a vehicle fit this spot type" (R3) needs only the vehicle and the spot type, so
Vehicle.fitsowns it. - "Pick the smallest fitting free spot" (R4) and "no same plate twice" (R6) need all spots and all open tickets, so
ParkingLotowns them. - "How much to charge" is a rule that is likely to change, so it sits behind
FeeStrategy. This is the axis of change from Level 5, module 6, and the strategy pattern of Level 4, module 1.
The links, as text:
ParkingLot has-many Spot (composition: spots exist only in the lot)
ParkingLot has-many Ticket (open) (a ticket is open from entry to exit)
ParkingLot uses FeeStrategy (injected)
ParkingLot uses Clock (injected)
Ticket refers to Vehicle, Spot
Notice what is not here: no Car, Bike and Truck subclasses. A vehicle's type changes only which spots fit and which rate applies, and both can be handled by an enum and a lookup. Three subclasses with no different behaviour would be the over-building warned about in Level 5, module 6.
Check yourself:
1. Which class owns the rule "pick the smallest fitting spot", and why?
2. Why is the fee behind an interface?
3. Why are there no Car and Truck subclasses?