Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| cohesion | how well a class's parts belong together (want high) | 1 |
| coupling | how much a class depends on others' details (want low) | 1 |
| SRP | one reason to change per class | 2 |
| OCP | extend by adding code, not by editing it | 3 |
| strategy (pattern) | interchangeable behaviour behind an interface | 3 |
| LSP | subclasses must be substitutable for their parent | 4 |
| ISP | many small interfaces over one fat one | 5 |
| DIP | business logic and details both depend on abstractions | 6 |
| dependency / dependency injection | what a class needs / passing it in from outside | 6 |
| constructor injection | dependencies passed through the constructor | 6 |
| composition root | the place that builds and wires objects | 6 |
| fake / test double | a stand-in implementation used in tests | 6 |
| DRY / KISS / YAGNI | one source of truth / keep it simple / don't build for imagined needs | 7 |
| Law of Demeter | talk to direct collaborators, not chains of strangers | 7 |
Cheat Sheet#
S "describe it without 'and'?" → split by reason to change
O "add the next variant without editing?" → interface + new implementation (strategy)
L "would a parent-type caller be surprised?" → fix hierarchy; compose instead
I "does anyone fake/throw on a method?" → split the interface
D "does business logic `new` up I/O?" → interface + constructor injection
Always inject: database, HTTP, email, file system, clock, randomness
Balance with: KISS · YAGNI · refactor when change actually arrivesinterface Notifier {
void send(String to, String msg);
}
class QuizService {
private final Notifier notifier;
private final Clock clock; // java.time.Clock
QuizService(Notifier notifier, Clock clock) {
this.notifier = notifier;
this.clock = clock;
}
}
// prod: new QuizService(new EmailNotifier(), Clock.systemUTC())
// test: new QuizService(new FakeNotifier(), Clock.fixed(Instant.parse("2026-11-20T10:00:00Z"), ZoneOffset.UTC))Try This Yourself#
Refactor
QuizManager. Write Part 0'sQuizManageras one class, then refactor it step by step intoScoreCalculator,ReportFormatter,ResultRepository(an interface with an in-memory implementation),NotifierandCreditRule. After each step, name the principle you applied.Add three features without editing old classes: a JSON formatter, a "teacher plan" credit rule, and an SMS notifier. If you had to edit an existing class, find which principle was missing.
LSP detective. Look at
java.util.List.of(1, 2, 3).add(4)— run it and read the exception. Explain in two sentences whether this is an LSP concern and why Java accepted the trade-off.