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

Tensions

Letters against letters, with a decision for each.

The point: senior questions are not about one letter. They are about two that disagree. Decide by the next change, and say what the other letter loses.

Case 1

S says split, O says: the engine must see all

Two alerts of one host should be joined into one message. Each sender could do it (then each must know the others), or the service could. Joining is about how alerts meet, so it belongs to the core. The service grows by one step and stays one actor.

Case 2

I says small, callers want many

Python
from typing import Protocolclass Sender(Protocol):    def send(self, to, text): ...class ReplyReader(Protocol):    def replies(self): ...class Conversation(Sender, ReplyReader, Protocol): ...      # for the one caller who needs bothclass Sms:    def __init__(self): self.lines = []    def send(self, to, text): self.lines.append("to " + to + ": " + text)    def replies(self): return ["on my way"]def ask(c: Conversation, to, question):    c.send(to, question)    return c.replies()[-1]print(ask(Sms(), "lee", "who takes it?"))
Small interfaces, joined where one caller needs both. Nobody implements the sum by force.
Edit and run it in your browser

Case 3

D says port, YAGNI says not yet

DependencyPort on day one?Why
Mail providerYesForeign, slow, costs money, fails in its own way
ClockYes, as a functionTests need to set the time
DatabaseA repository class. An interface when a second one existsThe test may use a real small database
Standard library list, JSONNoStable, fast, not yours to replace
Another class of the same ownerNoCall it directly
Another team's serviceYesOther release rhythm

Case 4

L against the real world

A read-only list looks like a list. But callers of a list may call add. Java's Collection answers with optional operations: the documentation allows UnsupportedOperationException. That is a contract which says: ask first. It keeps the number of interfaces small and moves the check from the compiler to run time. Python's collections.abc chose the other way: Sequence can read, MutableSequence can also write.

Read first, write on top

«interface»Readable+ get(i): T+ size(): int

1/4The smaller promise on top

Reading is what both can do.

extends (is a)implements

Segregation solves the Liskov problem: the class that cannot write is no longer asked to.

Case 5

O against a closed set

GrowsShapeWho helps you
Kinds (new senders)Interface and classesThe contract test
Operations (export, print, count)Closed set of kinds and functions with an exhaustive matchThe compiler lists the missing cases
NeitherPlain ifNobody needed
BothOpen functions or visitors with careExpect work
More on this in open/closed. Sealed types in Java and unions in TypeScript are not a defeat of O. They are the choice for the second row.

Two letters disagree