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
UML design
Draw it in the order you think it
The point: draw the coordinator first, then the owner of the scarce thing, then records, then the two edges of the system.
The design to reach: one coordinator, one owner of free spaces, plain records
1/6Start from the two verbs
The requirements have two sentences with a verb: a vehicle enters, a vehicle leaves. They become the two methods of one coordinator, ParkingLot. Everything a gate needs is there.
owns (dies together)usesimplementshas (lives on)
Read it
Arrows that people mix up
| Arrow | Means | Here |
|---|---|---|
| Filled diamond | Owns. They live and die together | ParkingLot and SpotPool |
| Empty diamond | Has. The part lives on | SpotPool and Spot |
| Dashed line, empty triangle | Implements an interface | HourlyPricing and Pricing |
| Solid line, empty triangle | Extends a class | Nowhere, and that is a choice |
| Plain arrow | Uses, knows about | Ticket and Spot |
Compare
The same picture as a tree
This is the diagram most textbooks draw: a Singleton lot, levels or floors, entry and exit panels, a tree of vehicles, a tree of spaces (motorcycle, compact, large, accessible, electric), a ticket with a status, a payment tree (cash, card, UPI) and a sign. Know it, because an interviewer may draw it. Then say what each box would do in code.
The large version
| Box in the large version | Keep? | What we do |
|---|---|---|
| Entry and exit panel | When they own hardware | Callers of enter and leave; see Entry and exit gates |
| Floor with a map of spaces | When it owns a lock | See Many gates and floors |
| Display board | As a listener | See Display board |
| Space tree: compact, large, accessible, electric | No | size and a set of features; see Extending it |
| Electric car with battery capacity | No | A need: must have a charger |
| Payment tree: cash, card, UPI | Yes, behind one interface | Implementations of Payments |
| Payment status: pending, completed, failed, refunded | Yes, outside the lot | See Payment and lost tickets |
| Ticket status | Yes, for pay first and drive later | See Payment and lost tickets |
Size
How much to draw
How much detail belongs on the board?
BreaksEverything, with getters
- How
- Every field, every getter, both directions of each relation.
- Limit
- Fifteen minutes gone and no code. The important line drowns.
- Choose when
- Never in an interview.
WorksBoxes with names and arrows
- How
- Seven boxes, seven lines.
- Limit
- Looks like a concept map. The interviewer cannot see who holds what.
- Choose when
- The first two minutes.
StrongState and public verbs
- How
- For each class with changing state: its fields and its public methods. Records get one line.
- Limit
- You must decide names early.
- Choose when
- Here. The fields show who owns what, the methods show the conversation.