AlgoViz

Design a parking lot

medium 90 min

Model 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

CandidateWhy notInsteadWhen it would be one
Car, Bus, MotorcycleThey differ in one valueAn enum with a fieldA vehicle with behaviour of its own
CompactSpot, LargeSpotThey differ in one valueSpot.sizeNever for size alone
LevelIt is a number and does nothingSpot.levelIt owns a lock, a sign or an opening time
GateIn software it is a caller of enterA thread or a requestIt owns hardware: barrier, printer, its own queue
ParkingManagerTwo owners of the same decisionsParkingLotNever: name the job instead
DisplayBoardIt shows what free() saysA caller, later a listenerSigns must be told, not ask
TicketPrinterNot part of the decisionOutsideIt has rules: reprint, out of paper
DriverWe never know who drivesThe plateAccounts and passes
"A level is only a number here, so I will not make it a class. If each level gets its own lock I will." That one sentence is worth more than the class.

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
Vehicle with canFitInSpot, 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

RelationKindTest: if A goes, does B go too?
Lot and poolCompositionYes. Nobody else has the pool
Lot and active ticketsCompositionYes, as a set. A printed ticket is paper
Pool and spacesAggregationA taken space is not in the pool and still exists
Ticket and vehicleUsesNo. The car drives on
Lot and pricingUsesNo. One rule may serve many lots
Hourly and PricingImplementsIt is a kind of rule