Notes
The problem
Here is the problem. You say the station has lockers, lockers have parcels, the courier also has parcels, and some lockers are cold. Every has is a different relationship, and none of the important questions are answered. Does a locker exist without its station? How many parcels can one locker hold? Is a cold locker a kind of locker, or something else? A class diagram answers all of these with a line and a number.
- "The station has lockers,
- lockers have parcels, the
- courier has parcels too,
- and some lockers are cold"
- questions left open:
- does a locker die with
- the station?
- how many parcels per locker?
- is a cold locker a locker?
The class box
Start with one class box. It has three parts. The top holds the class name. The middle lists fields, written as name, colon, type. The bottom lists methods with their parameters and return type. A minus sign in front means private, plus means public, a hash means protected and a tilde means package private. Interfaces get the word interface above the name, and abstract names are written in italics.
+-----------------------------+ | Locker | +-----------------------------+ | - code: String | | - size: Size | | - parcel: Parcel | +-----------------------------+ | + fits(p: Parcel): boolean | | + store(p: Parcel): void | +-----------------------------+ minus private, plus public
Association
The most common relationship is association, drawn as a plain solid line. It means one object keeps a reference to another and works with it over time. A locker holds a parcel. An arrowhead on the line shows the direction we can navigate: the locker knows its parcel, but the parcel does not know its locker. In Java, association is usually just a field of the other type.
Multiplicity
Multiplicity puts numbers at each end of a line. One means exactly one. Zero dot dot one means none or one. A star means any number, including zero. One dot dot star means at least one. So a station has one or more lockers, and a locker holds zero or one parcel. These numbers turn straight into code: zero dot dot one is a field that may be null, and a star is a list or a map.
- 1 exactly one
- 0..1 none or one
- * any number (0..*)
- 1..* at least one
- 2..4 between two and four
- station 1 to 1..* lockers
- locker 1 to 0..1 parcel
Whole and part
Two special kinds of association describe a whole and its parts. Composition, a filled diamond on the whole's side, means strong ownership. The station creates its lockers, and if the station is removed, its lockers go too. Each locker belongs to one station only. Aggregation, a hollow diamond, is a loose grouping. A courier route groups today's parcels, but when the route ends, the parcels still exist. If you are unsure, a plain association is always acceptable.
- composition: filled diamond
- station creates its lockers
- remove station, lockers go
- a part has one owner
- aggregation: hollow diamond
- a route groups parcels
- route ends, parcels remain
- parts can be shared
Inheritance
Inheritance, also called generalization, is a solid line with a hollow triangle pointing at the parent class. ColdLocker extends Locker. A cold locker is a locker, so it can be used anywhere a locker is expected, and it changes one rule: it also accepts chilled parcels like medicine or groceries. The triangle always points up, toward the more general class. Check the is a test before you draw it.
Realization
Realization is how a class implements an interface. It looks like inheritance but the line is dashed: a dashed line with a hollow triangle pointing at the interface. SmsNotifier realizes the Notifier interface. Tomorrow an email notifier or a push notifier can realize it too. In the diagram the interface is marked with the word interface in double angle brackets above its name.
Dependency
Dependency is the weakest relationship, a dashed line with an open arrowhead. It means one class uses another briefly, as a method parameter, a local variable or a return type, but does not keep it in a field. The station's deposit method receives a PinGenerator, asks it for one pin, and lets it go. If PinGenerator changes, the station might be affected, which is all a dependency arrow promises.
The whole diagram
Put together, the whole design fits on one screen. The station owns one or more lockers by composition, keeps a Notifier by association, and uses a PinGenerator during deposit. ColdLocker extends Locker. SmsNotifier realizes Notifier. Each locker holds zero or one parcel, and a courier route aggregates parcels. An interviewer can now see ownership, choices and counts before you write a single line.
Diagram to code
Each line type maps to plain Java. Composition: the station creates its lockers inside its constructor and never hands them out for others to own. Association: a field, like the notifier. Dependency: only a method parameter, like the pin generator in deposit. Inheritance is extends, realization is implements. Aggregation is a field holding objects that were created elsewhere and passed in, like the route's list of parcels.
// composition: created inside lockers = List.of(new Locker(..), ...) // association: a field private final Notifier notifier; // dependency: only a parameter String deposit(Parcel p, PinGenerator g) // inheritance class ColdLocker extends Locker // realization class SmsNotifier implements Notifier
Run it
Watch one deposit. The courier, working through the route's parcels, hands parcel P twelve to the station. The station walks its own lockers, finds the first one that fits, and stores the parcel there. It asks the pin generator for a fresh pin, keeps the pin against that locker, and tells the notifier to text the pin to the customer. Every arrow in the diagram was used once.
Drawing it fast
In an interview, draw it in layers. First, boxes with just the names of the main nouns. Second, the lines: is it a kind of, does it own, or does it just use. Third, numbers on the important ownership lines. Fourth, only the fields and methods that carry business rules, like fits and deposit. Fifth, mark the policies that might change as interfaces. Skip getters, setters and every private detail. A clear sketch beats a complete one.
- 1 boxes for the main nouns
- (name only at first)
- 2 lines: is-a, has-a, uses
- 3 numbers on key has-a lines
- 4 add the few fields and
- methods that carry rules
- 5 interfaces for policies
- skip getters, setters and
- every private field
Common mistakes
Watch for the usual mistakes. The triangle always points at the parent or interface, never at the child. Do not use inheritance for has a relationships: a station is not a locker. Do not put filled diamonds on everything; most relationships are plain associations. Always add numbers on the key lines. And do not list every getter. When you are unsure which symbol fits, a plain line with a clear label is better than a wrong symbol.
- triangle pointing at the
- child (it points at parent)
- inheritance for "has-a"
- (a Station is not a Locker)
- composition everywhere
- missing multiplicity
- too much detail, no story
- when unsure: plain line
- plus a label