Draw the classes and the arrows
After this: Draw the parking lot's class diagram and give a reason for each line and each diamond.
On this page · 6 sections · 3 checks
Lesson 2 gave you a list: nine classes and an enum, each with its fields and methods. A list does not show how the classes connect. Which class owns which? Which one only points at another? A shows that at a glance, and this lesson draws one for the lot.
The lines you will need
Four kinds of line from "Reading and drawing class diagrams" are enough for this design:
| Line | Say it as | Drawn as |
|---|---|---|
| inheritance | "is a" | solid line, hollow triangle at the parent |
| "owns" | filled diamond at the owner | |
| "holds, but does not own" | hollow diamond at the holder | |
| "knows about" | plain line, open arrowhead |
Build it in six steps
The diagram below starts empty. Press Run it to add one group at a time, in this order: the vehicles, the spot, the level, the lot, the ticket and the fee rule. Read each step's note before you go on. The method names follow the code language you have chosen; the types are written the Java way, as in every diagram of this learning path.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
A reason for every line
Each line is a choice, and you should be able to say why you made it. Here are the reasons, in the order of the steps:
- Bike, Car and Truck to Vehicle: inheritance. A bike is a vehicle. Each child answers
size()andrate()in its own way, so the rest of the program can treat every vehicle alike. - Spot to Vehicle: association, labelled
0..1. A spot knows which vehicle is on it, if any. It does not own the vehicle: the vehicle arrives, stays a while and drives away. - Level to Spot: composition. Ask the second question from "Reading and drawing class diagrams": does the part live on alone? A spot is built for one level, in the same line that builds the level. It is never moved to another level, and it has no use without its level. So the level owns it. A ticket may point at a spot for a while, but it does not own it.
- ParkingLot to Level: composition. The same reasoning, one size up: each level is built for this lot, in the line that builds the lot, and never moves to another lot.
- ParkingLot to Ticket: composition. The lot calls the
Ticketconstructor itself, inpark, and keeps the open tickets until each vehicle leaves. - Ticket to Vehicle and Ticket to Spot: association,
1each. A ticket records exactly one vehicle and one spot. It points at them; it does not own them. - ParkingLot to FeeCalculator: aggregation. The fee rule is made outside and handed to the lot when the lot is built. The lot uses it but did not make it, so another rule can be handed in instead.
The small labels at the line ends are the : how many objects can be at that end. A lot has one or more levels (1..*). A spot holds no vehicle or one (0..1). A lot can have any number of open tickets, including none (0..*).
Size sits on its own as a small box marked «enum», with its three values. The size field of Spot and the size() method of Vehicle both name it.
Why the fee rule is its own class
Lesson 2 gave FeeCalculator its own class because fee rules change. The diagram shows the result. ParkingLot points at "some fee rule", through the hollow diamond. When the owner asks for a weekend rate, you write a new fee class and hand that to the lot when you build it; no line inside ParkingLot changes.
This is the pattern from "Three patterns you will meet first". It also follows the first SOLID principle: ParkingLot has one job, which is to find spots and keep tickets, and the fee rule is someone else's job. The final challenge adds the weekend rule.
Check yourself
Which line joins Level and Spot, and why?
Where does the rule "pay the rate for every started hour" live in the design, and why there?
The line from Spot to Vehicle is labelled 0..1. What does that say?
Other good drawings
- Draw the parking lot's ten classes and ten lines, starting from the vehicles and ending with the fee rule.
- Use composition for parts that belong to one whole for life (spots in a level, levels in a lot, tickets the lot makes).
- Use aggregation for a part made outside and handed in (the fee rule), and association for a field that only points at another object (a ticket's vehicle and spot).
- Read the multiplicity labels: 1, 0..1, 1..* and 0..*.
- Explain why the fee rule is its own class: one job per class, and a Strategy that can be swapped.