Foundations · 8 min

SOLID: the reasons behind the patterns

Five principles, each tied to the patterns it gave rise to — plus counterexamples so it doesn't turn into dogma.

S — Single Responsibility

A class has one reason to change. An OrderService that calculates totals, validates, calls the payment gateway, and formats invoices will get edited by accounting, by legal, by the payments team, and by marketing.

Related patterns: Strategy (pull out algorithms), Facade (one front door, not one pile of logic), Command (separate "the job" from "who does it").

tssrp.ts
// One class, three reasons to change: wrong.
class OrderService {
  place(order: Order) { this.validate(order); this.charge(order); this.invoice(order); this.log(order) }
  // validate() changes when the rules change. charge() changes when the gateway changes. invoice() changes when accounting changes.
}
SRP isn't "every function is 5 lines" — it's "every module serves only one source of change".

O — Open/Closed

Open for extension, closed for modification. Add features by adding code, not by editing code that already works. This is exactly why Strategy + a registry exists: a new fee type = one new file.

In JS, you usually get there with maps/hooks/plugins rather than inheritance.

tsocp.ts
// Closed: nobody edits this when a new type is added.
const renderers: Record<string, { render: (x: Report) => string }> = { csv, json, html }

// Open: adding "pdf" is adding one key.
renderers.pdf = { render: toPdf }

L — Liskov Substitution

Calling code must work correctly with every implementation of a contract, without knowing which one it's holding. The classic violation: Square extends Rectangle — Rectangle's setters make Square wrong.

Composite is the pattern that violates it most often: leaf.add(child) throwing an exception means the leaf can no longer stand in for a composite. The tell: calling code has to do if (x instanceof Leaf).

I — Interface Segregation

Many small, focused contracts beat one big contract that implementations have to fake. Forcing everything to implement CrudRepository and then throwing "not implemented" is the symptom.

In TS, the equivalent: instead of one 12-method interface, split it into { readable }, { writable }, { searchable } and accept exactly what you need.

D — Dependency Inversion

Business code depends on contracts, not on details. This is why Strategy receives its strategy from outside instead of doing new ExpressShipping() internally.

The practical payoff: fast tests (pass a fake), swapping implementations doesn't ripple, and business code reads like documentation.

tsdip.ts
interface PaymentGateway { pay(amount: number): Result }

// Business logic only knows the contract. Testing? Pass a fake. New vendor? Add a class.
class Checkout {
  constructor(private gw: PaymentGateway) {}
  async submit(order: Order) { return this.gw.pay(order.total) }
}

new Checkout({ pay: async amount => ({ ok: amount < 1_000_000 }) }) // a fake, no mocking library needed

And the cost of dogma

SOLID is a tool for lowering the cost of change, not a certificate. If your project doesn't change along those axes, every "just in case" interface is a file the next person has to read only to never use.

A practical rule: build an abstraction once you have at least two real implementations, or when you know for sure the second one is coming (tests, multiple vendors, multiple platforms). With only one, write it directly — a clear function name is enough.