Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / SOLID Principles

    The Whole Story

    The whole module retold as one story — no new facts, a fast revision of SOLID Principles.

    Extra: Revision2 min read

    Code is changed far more often than it's written, and the one-class-does-everything QuizManager made every change risky. Two ideas measure the damage: cohesion — does everything in a class belong together, like a well-sorted drawer? — and coupling — is a class plugged into its neighbours or wired into the wall? (Ch 1).

    Single Responsibility splits the class so each has one reason to change, like chef, waiter and cashier: scoring, formatting, storage, notification and billing each get their own home, and a CSV export becomes a new class rather than an edit (Ch 2). Open/Closed makes the code a power strip: new behaviour plugs in as a new implementation of an interface — a new CreditRule for exam week — while tested code stays closed (Ch 3). Liskov Substitution insists every substitute teacher keeps the parent's promises; the square that secretly changes its width breaks correct code, so "is-a in real life" isn't enough (Ch 4). Interface Segregation replaces the 80-button remote with small interfaces, so nobody implements methods they can't support (Ch 5). Dependency Inversion makes both sides depend on the plug standard: QuizService depends on a Notifier interface and receives its implementation through constructor injection, so the same code sends real email in production and records messages in a test (Ch 6).

    Around them: DRY for one source of truth, KISS and YAGNI to stop over-building, and the Law of Demeter against long chains (Ch 7). And the judgement that ties it together: start simple, refactor toward SOLID when real change arrives, always inject I/O, and name the principles out loud in design interviews (Ch 8).

    These principles are the "why" behind the design patterns in Design patterns (coming soon) and every LLD answer in LLD interview questions (coming soon).