SOLID in one page
medium 45 minFive principles for classes that can be changed without fear: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion.
Watch lesson5:16
Check yourself
One question at a time. Each answer explains why.
Question 1 of 13
`OrderManager` writes to Postgres, formats JSON and sends SMS. Which principle is violated first of all?
`ReadOnlyList` extends `List` and throws in `add()` and `remove()`. What is the main problem?
What is dependency inversion about?
Which statement about preconditions is right?
Why can a fat interface lead to a Liskov violation?
What does single responsibility count?
A service receives a `MySqlClient` in its constructor and writes SQL in its rules. What is true?
A pager carries only 20 characters. `Sender` promises the whole text. Best step?
You need retry around three senders. Where?
An interface has one implementation, no outside world, and nobody plans a second. What now?
During a deployment v2 of a producer renames a value that v1 consumers read. Which letter describes the fault?
Seven interfaces with one method each are always used together by one caller. What is better?
The interviewer adds a requirement in the last ten minutes. Which answer shows the most?
Pick an answer to see why.
Not quite. Database people, API consumers and the owners of messages each have a reason to change it.
Not quite. If the parent's contract promises add, the child breaks it. A contract with optional operations would say so openly.
Not quite. Injection and containers are techniques that help.
Not quite. Callers know only the parent's demands, so a child may be more generous, not stricter.
Not quite. Then they raise or do nothing, and callers are surprised.
Not quite. Size is a hint, actors are the measure.
Not quite. The rules know SQL and the client. A port in the words of the rules would turn the arrow.
Not quite. It cannot keep the promise, so it should not make it.
Not quite. One owner, no edit of service or senders, and it can be left out in the wiring.
Not quite. It costs a jump and gives no freedom. It can return with the second kind.
Not quite. The new version cannot stand in for the old one: it gives less than was promised.
Not quite. Segregation is per caller, not per method.
Not quite. The change itself is the test of the design.
Flashcards
Say your answer, then reveal it.
Card 1 of 14
What is the core of the open/closed principle?
Open for extension, closed for modification: new behaviour arrives as classes behind a contract, along an axis you chose, and tested code is not edited for it.
Say your answer, then reveal it.
What breaks the Liskov substitution principle?
A subclass that cannot stand in for its parent: unexpected exceptions, stronger demands, weaker results, broken invariants.
Say your answer, then reveal it.
How do interface segregation and dependency inversion work together?
ISP keeps contracts small and made for the client. DIP lets the rules depend on these contracts, and the details implement them.
Say your answer, then reveal it.
Single responsibility in one line?
One class, one group of people who may ask for a change.
Say your answer, then reveal it.
Interface segregation in one line?
Nobody depends on methods they do not use.
Say your answer, then reveal it.
Dependency inversion in one line?
Rules own the abstraction, details depend on it.
Say your answer, then reveal it.
Injection or inversion?
Injection hands something in. Inversion decides who owns the contract.
Say your answer, then reveal it.
Why does a fat interface break L?
Classes must implement what they cannot do, so they raise or do nothing.
Say your answer, then reveal it.
What does a broken L do to O?
The core needs a special case for that kind, so it is edited again.
Say your answer, then reveal it.
May a subtype accept less or more than its parent?
More. Preconditions may become weaker, never stronger.
Say your answer, then reveal it.
Which letter first?
The one that makes the next wish cheap.
Say your answer, then reveal it.
L between two versions of a service?
The new one may accept more and promise more, never less.
Say your answer, then reveal it.
When not to use SOLID?
Scripts, prototypes, one kind with one owner, closed sets, measured hot loops.
Say your answer, then reveal it.
Who formed the acronym?
Michael Feathers, about 2004, from principles collected by Robert C. Martin.
Say your answer, then reveal it.

These cards come back in Today, just before you would forget them.