Part 6 of 7 · 2 min read
Coupling
Every arrow is a dependency. Keep them few and pointing the right way.
Coupling and the direction of arrows
Each line in a diagram means that one class needs the other. More lines mean a design that is harder to change. A rule of thumb (common practice, taken up in Level 2): arrows should point from the changeable to the stable, and from concrete classes to interfaces. In the room system, BookingService points at Booking and Room. They should not point back at the service.
A made-up count: a design with 8 classes and 20 arrows is more tangled than one with 8 classes and 9. When a class has more than about five arrows, check whether it has too many jobs.
Common mistake
Drawing every field as an arrow. Draw the links that matter to the story, as Level 0 advised.
Interview angle
Answer in your head first, then open it.
What is the difference between aggregation and composition?
Composition owns the part and the part ends with the whole. Aggregation holds parts that live on elsewhere. Say which definition is in use.
What is the difference between association and dependency?
Association is held over time in a field. Dependency is a short use, such as a parameter.
How do you decide?
Ask whether the part outlives the whole, and who creates it.
Check yourself: 1. In which direction should arrows point? 2. When should a class have its arrows checked? 3. Why say which definition of aggregation is used?
Words you will meet
| Word | Meaning |
|---|---|
| Aggregation (GoF) | One object owns another, with identical lifetimes |
| Acquaintance | One object only knows another |
| Association | A lasting link between two classes |
| Composition | A whole that owns parts which end with it |
| Dependency | A short use of another class |
| Multiplicity | How many objects sit at each end of a link |
| Run-time structure | The network of objects while the program runs |