Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
Entities
What is a class, and what is not
The point: the quality of a design shows in the classes you did not write. Say each rejection aloud, with the reason.
Keep
Seven that earn their place
ParkingLot
Changing state: active tickets, plates inside. Owns the order of things.
SpotPool
Changing state: what is free. One rule: which one to hand out.
Spot, Vehicle, Ticket, Receipt
Facts that travel together. Records, unchanging, no behaviour.
Pricing
Behaviour that differs and changes. An interface.
Payments
A machine somebody else owns. An interface, so that tests can pass a fake.
SpotSize, VehicleType
Fixed sets of values. Enums, one with a field.
Reject
Eight that do not, and what to say
| Candidate | Why not | Instead | When it would be one |
|---|---|---|---|
Car, Bus, Motorcycle | They differ in one value | An enum with a field | A vehicle with behaviour of its own |
CompactSpot, LargeSpot | They differ in one value | Spot.size | Never for size alone |
Level | It is a number and does nothing | Spot.level | It owns a lock, a sign or an opening time |
Gate | In software it is a caller of enter | A thread or a request | It owns hardware: barrier, printer, its own queue |
ParkingManager | Two owners of the same decisions | ParkingLot | Never: name the job instead |
DisplayBoard | It shows what free() says | A caller, later a listener | Signs must be told, not ask |
TicketPrinter | Not part of the decision | Outside | It has rules: reprint, out of paper |
Driver | We never know who drives | The plate | Accounts and passes |
How do you model vehicle types?
BreaksA string and an if chain
- How
if kind == "car" or kind == "truck"wherever it matters.- Limit
- A typo is a new vehicle. Each new type is a hunt through the code.
- Choose when
- Never.
WorksAn abstract class with subclasses
- How
VehiclewithcanFitInSpot, three subclasses, each with its own answer.- Limit
- Three classes for one number. A new type is a new file that says the same with another value.
- Choose when
- When types really behave differently: a trailer that takes two spaces, a car that charges.
StrongAn enum with the one fact that differs
- How
CAR(MEDIUM). The fit rule is one comparison in one place.- Limit
- Grows awkward if types gain behaviour. Then move to a need with features.
- Choose when
- Here. And say what would make you change your mind.
Follow-up
Abstract class or interface, if they ask for the tree
The classic answer is a fair one and you should be able to give it. Vehicle as an abstract class has shared state (plate, size) and shared code (getters), and forces each subclass to answer canFitInSpot. An interface is a promise without state. Subclasses may be used wherever the base is expected, which is Liskov substitution: a Motorcycle that says yes to every space is still a Vehicle.
Then say what you would miss: Motorcycle, Car and Bus would each contain one constant. The day a bus needs five spaces in a row, canFitInSpot(spot) can no longer answer for one space alone, and the tree does not help. The need is the thing to model.
Relations
| Relation | Kind | Test: if A goes, does B go too? |
|---|---|---|
| Lot and pool | Composition | Yes. Nobody else has the pool |
| Lot and active tickets | Composition | Yes, as a set. A printed ticket is paper |
| Pool and spaces | Aggregation | A taken space is not in the pool and still exists |
| Ticket and vehicle | Uses | No. The car drives on |
| Lot and pricing | Uses | No. One rule may serve many lots |
| Hourly and Pricing | Implements | It is a kind of rule |