AlgoViz

SOLID in one page

medium 45 min

Five principles for classes that can be changed without fear: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion.

Watch lesson5:16

Chapter mnemonic

Five SOLID design questions

One job. Bolt on extras. Swap a part. Pack small kits. Fit a standard socket.

A replacement part fits, but it breaks behavior the caller relies on. Which SOLID principle is at stake?

Liskov substitution. A subtype must honor the behavioral contract of its supertype.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Who asks, what is added, can I trust it, what do I need, who owns it?

A marble run on a board with holes. S: one piece, one job. O: hang one more. L: what goes in must come out. I: one peg if you need one. D: the board owns the holes, not the funnel.

SOLID in one page in one picture

Cheat sheet

S
one group of people may ask for a change
O
new kind = new code, on a chosen axis
L
never ask for more, never give less, no surprise
I
a caller sees only what it uses
D
the rule owns the port, the detail implements it
Chain
S and I cut, D turns arrows, O gains, L keeps
DI
handing things in: a technique, not the principle
Order
the next wish decides
Stop
one kind, one owner, closed set

Spot it when you see

  • A class named Manager
  • The same switch again
  • isinstance in the core
  • An empty or raising method
  • new inside a rule
  • A test that needs a server

Tricks that stick

  • Five words: Split, Open, Like (a subtype acts like its parent), Interfaces, Direction.
  • One hand, five fingers: each does one job, and you grip only when they work together.
  • For L ask: would a caller notice the swap?

Do not forget

  • All five at once
  • An interface per class
  • Contracts owned by the detail
  • Injection taken for inversion
  • Promises only in the signature
  • Reciting letters instead of pointing at lines