Course map

Topic 044 / Core

SOLID principles

Chapter 4.1 · Object-Oriented Design and SOLID

What and why

OOD (→ 43) says to group state with behavior, but gives no test for whether a given grouping will survive the next change. SOLID is five checkable pressures on a class or module's design, each catching a specific kind of brittleness before it becomes a rewrite. Builds on: → 43.

Diagram

Letter Principle One-sentence test Smell it catches
S Single Responsibility Does this class have more than one reason to change? A class edited for unrelated reasons by unrelated people
O Open/Closed Can I add behavior without editing existing, tested code? A growing if/else chain on type
L Liskov Substitution Can every subtype be used wherever the base type is expected, with no surprises? A subtype that throws or no-ops on an inherited method
I Interface Segregation Does a client depend on methods it never calls? A "fat" interface forcing unrelated implementations
D Dependency Inversion Does high-level policy import low-level detail, or the other way around? A domain class importing a specific database driver

How it works

  1. Single Responsibility — split a class when its reasons to change diverge (e.g. separate assignment logic from notification logic in a TaskService).
  2. Open/Closed — replace a type-switch with polymorphism so a new case is a new class, not an edit to existing, already-tested code.
  3. Liskov Substitution — if a subtype must reject part of its parent's contract (throwing on a method the base type promises), it isn't really a subtype; stop inheriting and compose instead (→ 43).
  4. Interface Segregation — split a wide interface into smaller, client-specific ones so no implementer is forced to stub out methods it doesn't need.
  5. Dependency Inversion — make the high-level module define the interface it needs; the low-level module implements it. The direction of the import statement should point from detail toward policy, never the reverse.

Practical example

(illustrative). A TaskService.notify() method with an if (channel == "email") ... else if (channel == "sms") ... chain violates Open/Closed: every new channel means editing and re-testing this method. Replacing the chain with a NotificationSender interface and one implementation per channel (Strategy, → 47) lets a new channel be added as a new class.

When to use it

  • Use when: a concrete pain shows up — a second implementation is needed, a class is hard to test because it constructs its own dependencies, or a subtype is breaking callers.
  • Avoid when: applying a letter preemptively, with no second implementation and no test friction — e.g. Interface Segregation splitting an interface nobody else implements.
  • Failure scenarios: an interface with exactly one implementor and no test seam — the same over-abstraction warned about in → 43, now wearing a design-principle label; Dependency Inversion applied where there will only ever be one implementation.
  • Simpler alternative: write the straightforward version first. Apply a letter only once the pain it's meant to prevent actually appears.

Practice

(15–20 min; no lab services — any language). Classification drill: given four short code snippets (a god class doing validation, persistence, and emailing; a type-switch on a payment provider; a subtype that throws NotImplementedError on an inherited method; a five-method interface with one implementer using two of them), name the SOLID letter each violates and the smallest fix. Done when every snippet is matched to a letter and a minimal fix you could defend in review.

Key takeaway

SOLID is five checkable questions, not law — apply a letter only when its matching pain is real. Done when you can: look at a class and state which letter, if any, it is currently straining against. Used next in → 47 (patterns), → 49 (dependency inversion generalized from a class to a module), → 51 and → 52 (clean and hexagonal architecture apply Dependency Inversion at the whole-application scale).

References

None — this topic makes no external claims.

Source directory

Search the guide