Handbooks / OOP & Design Patterns / Chapter 5
Coupling and Cohesion
7 pages · ~5 min✓ Reviewed
Builds on SOLID Principles. Next up: Creational Patterns.
Coupling and Cohesion Mental Model
1. Definitions
- Coupling = how much one module must know about or change with another
- Cohesion = how closely a module's own parts belong together
- Goal: low coupling between modules, high cohesion within one
- They reinforce each other: focused modules need fewer links
2. The Two Tests
| Coupling | Cohesion | |
|---|---|---|
| Ask | Change A: how many other modules must change? | Job in one sentence: any 'and'? |
| Bad | Many ripples | Needs 'and' |
| Good | Few or none | One clear job |
3. Low-Cohesion Smell
Utils,Manager,Helperclasses- They grow unrelated methods over time
- Name needs 'and'? Split it
Remember
Low coupling outside, high cohesion inside. One change should touch one place.
Types of Coupling
1. Worst: Reaching In
- Content (worst): reads or edits another module's internals
- Private state access, reflection hacks
- Common: modules share global mutable state
- Any change can break any reader
2. Flags and Fat Objects
| Type | Smell | Fix |
|---|---|---|
| Control | caller passes a flag: render(true) | separate methods, or polymorphism |
High Cohesion and the Law of Demeter
1. Cohesion levels
| Level | Parts grouped by |
|---|---|
| Functional | one well-defined task (best) |
| Sequential | output of one feeds the next |
| Communicational | same input data |
| Logical | same category, chosen by a flag |
| Coincidental | random grouping (worst) |
- Cohesion = Single Responsibility Principle at module level
Composition over Inheritance and Dependency Injection
1. Inheritance vs Composition
- Inheritance is the tightest class coupling: the subclass depends on the parent's implementation
- Fragile base class: a parent change silently breaks subclasses
- Composition: hold a collaborator as a field and delegate to it
| Inheritance | Composition | |
|---|---|---|
| Relation | is-a | has-a |
| Depends on | parent's internals | an interface |
How SOLID and Patterns Serve Low Coupling
1. SOLID: depend on abstractions
- High-level codeowns the interface
- Abstractione.g.
Notifier - Low-level detailimplements it
Coupling and Cohesion Cheat Sheet
1. The Two Ladders
| Rung | Coupling | Cohesion |
|---|---|---|
| Worst | Content | Coincidental |
| Common | Logical | |
| Control | Communicational | |
| Stamp | Sequential | |
| Best | Data, then message | Functional |
Check yourself
A caller writes export(report, true), where true picks PDF over HTML. Which coupling is this, and what fixes it?
- Control coupling: the caller steers the callee's logic with a flag
- Fix: separate
exportPdfandexportHtml, or pass a format object (polymorphism)
order.getCustomer().getAddress().getCity() is used in many places. Address is then redesigned. What breaks, and why?
- Every caller with this chain, since each one knows the internals of three classes
- Fix: tell, don't ask. Add
order.shippingCity()that delegates