Skip to content

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

    Java / SOLID Principles

    Using SOLID Sensibly

    SOLID is a set of tools for managing change, not rules to maximise. Applying every principle everywhere produces the opposite problem: a

    Chapter 8 of 82 min read

    SOLID is a set of tools for managing change, not rules to maximise. Applying every principle everywhere produces the opposite problem: a tiny feature spread across nine interfaces and twelve classes that nobody can follow.

    A practical approach:

    1. Start simple (KISS, YAGNI). One class is fine while it's small and has one reason to change.

    2. Refactor when change arrives. The second time a new plan type forces you to edit the if chain, extract the CreditRule interface (OCP). The first time a test needs to avoid sending email, inject the Notifier (DIP).

    3. Always inject I/O (DIP) — database, network, email, clock. This one pays off from day one because it makes testing possible.

    4. Use inheritance only when LSP holds; otherwise compose.

    5. In interviews, name the principle as you use it: "I'll put payment behind a PaymentGateway interface so the booking service doesn't depend on Stripe directly — that's dependency inversion, and it lets us add Razorpay without changing the service."

      Principle   One-line test you can ask about any class
      ─────────   ──────────────────────────────────────────────────────────────
      S           "Can I describe this class without saying 'and'?"
      O           "Can I add the next variant without editing this class?"
      L           "Would a caller of the parent be surprised by this subclass?"
      I           "Does any implementer have to fake or throw on a method?"
      D           "Does this business logic call `new` on a database/email/HTTP class?"