SOLID without dogma
- Lesson 4 of 32
- 14 min
- Beginner
- 12 steps
Split a class that does everything, letter by letter, and learn to tell when a principle helps and when it only adds files.
- Principle
- SOLID
- request
- response
- failed request
Step 1 of 12
Problem
One class, three reasons to change
main.ts calls OrderService.confirm(). That method saves the order to the database, decides whether to notify by email or SMS, and also renders the PDF invoice.
It works. The trouble comes with changes: the data team changes a table, marketing wants WhatsApp notifications and accounting asks for a new invoice layout. All three touch the same class, and each change can break the others.
Step 2 of 12
Concept
SOLID: five questions, not five laws
SOLID groups five object-oriented design principles. Robert C. Martin collected them in his paper "Design Principles and Design Patterns" (2000) and, by his own account, Michael Feathers proposed around 2004 ordering them to spell the word.
Use them as code review questions: how many reasons does this class have to change? Can I add a case without editing it? Does this subtype do what it promises? They are not rules to apply always and to the maximum.
Step 3 of 12
Solution
S: one reason to change per class
Single responsibility does not mean "one class, one method". A practical reading: if two different people can ask you to change the same class for different reasons, that class has two responsibilities.
We move the SQL to OrderRepo and the invoice to InvoicePdf. OrderService becomes a coordinator: it calls each piece in order and knows how none of them works.
Now a table change touches OrderRepo and nothing else, and its test does not need to render a PDF.
What changed
- OrderRepo: saves the order
- InvoicePdf: renders the invoice
- OrderService only coordinates
Step 4 of 12
Solution
O: open to extension, closed to editing
Before, every channel was another branch of the if inside OrderService: adding WhatsApp meant editing, and testing again, a class that already worked.
We define the Notifier interface with a single method, send(). EmailNotifier and SmsNotifier implement it (the arrow with a triangle points to the interface) and OrderService only calls send().
A new channel is a new class and OrderService stays untouched: it is closed to editing and open to extension.
What changed
- Notifier (interface)
- EmailNotifier and SmsNotifier
- The per-channel if in OrderService
Step 5 of 12
Problem
L: the substitute that throws
In Java, AbstractList lets you build a list that cannot be modified by implementing only get() and size(). Its default add() throws UnsupportedOperationException.
FixedList does exactly that and, since it extends AbstractList, it is a List. Checkout takes a List and calls items.add("gift"). The compiler is fine with it; at run time, the call fails.
The bug is not in Checkout, which used the contract exactly as List describes it. It is in a subtype that does not keep that contract.
What changed
- Checkout takes a List
- ArrayList and FixedList implement List
Step 6 of 12
Solution
L: a subtype must be able to take the parent's place
The idea comes from Barbara Liskov's work, formalised with Jeannette Wing in "A Behavioral Notion of Subtyping" (1994): a subtype must behave like its base type in everything the client expects from it.
If FixedList cannot add, it should not promise to. We give it a smaller contract, ReadList, with get() and size(). Whoever needs to add asks for a List and gets an ArrayList, which keeps the promise.
The practical test: can you pass the subtype to any code written for the parent without that code having to know? If not, the inheritance is lying.
What changed
- ReadList: get() and size()
- FixedList implements ReadList
- FixedList as a List
Step 7 of 12
Problem
I: depending on methods you do not use
OrderStore has five methods: find, save, delete, export CSV and import CSV. Report only shows orders, so it uses find() and nothing else.
Even so, Report depends on all five. Its test double has to implement five methods to use one, and a change to importCsv() makes you review code that never imports anything.
What changed
- Report uses OrderStore
- SqlOrders implements OrderStore
Step 8 of 12
Solution
I: an interface shaped by who uses it
We split OrderStore by who uses it: OrderReader with find() for Report, and OrderWriter with save() and delete() for Admin. The CSV methods go to a third interface when someone needs them.
SqlOrders implements both, so database access does not change. What changes is what each client sees: the Report fake now has a single method.
What changed
- OrderStore becomes OrderReader
- OrderWriter for Admin
Step 9 of 12
Technique
D: the arrows point to the abstraction
Look at the arrows: OrderService depends on Notifier, and EmailNotifier also points to Notifier because it implements it. Nothing in the business logic points to the class that sends emails.
That is dependency inversion: the high-level code (confirming an order) and the low-level code (talking to an email provider) both depend on an abstraction, and the code that uses it defines it.
Picking the implementation is the job of main.ts, the only place that writes new EmailNotifier(). In a test you pass a fake notifier and nothing is sent.
What changed
- main.ts picks the implementation
Step 10 of 12
Technique
Where you meet it: NestJS and Angular
NestJS and Angular do the job of main.ts for you. In NestJS you declare the service as a provider and ask for it in the constructor: the framework creates OrderService and hands it to you. In Angular you ask for it with inject() or in the constructor.
OrdersController never writes new OrderService(). Which implementation it gets is decided outside it, so changing it does not mean editing the controller.
What changed
- main.ts → a NestJS or Angular OrdersController
Step 11 of 12
Trade-off
When SOLID costs more than it gives
Every principle adds types and hops. A Notifier with a single implementation and no test that swaps it is ceremony: one more file to read and no benefit today.
In a short script or a prototype you will throw away, a class with two responsibilities is fine. Split it when the second reason to change shows up, not before.
The principles also clash: splitting for the S and for the I multiplies files, and the reader pays in navigation.
What changed
- SmsNotifier: this example has a single channel
Step 12 of 12
Summary
Five questions for your next review
S: how many reasons does this class have to change? O: can I add a case without editing it? L: does the subtype keep everything the parent promises?
I: does this client depend on methods it does not use? D: does the business logic point to an abstraction or to a detail?
And a sixth: does the answer justify the complexity it adds? If not, keep the code simple and ask again when it changes.
← → step · R replay · Space pause
You will learn
- The question each SOLID letter asks (covered)
- How to split a class that changes for several reasons (covered)
- How to add a new case without editing working code (covered)
- How to recognise a subtype that breaks its callers (covered)
- Why it pays to depend on small abstractions (covered)
- When following SOLID costs more than it gives (covered)