THE SOLID HARBOUR · CHAPTER 5 OF 5
Keep high-level policy dependent on an abstraction it needs, rather than on the details of a particular implementation.
01 / MEET THE PRINCIPLE
First understand the idea. Then explore why it helps, before bringing it to life in the harbour.
WHERE THE IDEA BEGAN
Robert C. Martin described dependency inversion among the principles in his 2000 paper. It reverses the usual source-code dependency from business policy toward implementation details by introducing an abstraction.
02 / WHY IT MATTERS
Useful where business rules meet databases, email services, payment gateways or hardware. A boundary can also allow a controlled test double.
The policy can remain stable while the implementation changes. Both the policy and the adapter refer to the contract, while a composition step chooses what is connected.
Changes in infrastructure spread into business rules. Tests become slow or awkward because they must construct real external services.
KNOW THE TRADE-OFF
Dependency injection is one wiring technique; it is not the principle itself. A DI container is optional. Avoid wrapping every stable value or simple utility in an interface without a useful boundary.
Next, step into the story and see this problem through a familiar situation.
03 / ONCE UPON A CHANGE
The lighthouse’s signalling routine creates and operates one particular diesel generator. Switching to a battery means editing the signalling code. Worse, testing the signal drags a real generator into the test. The purpose of the light has become tangled with its power supply.
The light defines the power capability it needs. Generators and batteries provide that capability. A separate setup step plugs the chosen source into the light.
Turn to “Try it” to change the design and see what happens.
THE HARBOUR LIGHTHOUSE
04 / PULL THE PAPER TAB
Signal code constructs a concrete DieselGenerator.
JavaScript is needed for the simulation. The story, code and practical notes remain available below.
Choose a scenario, then try the change.
An illustrative model, not a benchmark or a claim that every design change causes a bug.
05 / FROM STORY TO CODE
class Lighthouse { private readonly DieselGenerator power = new DieselGenerator(); public void Signal() { power.Supply(); } }
A MORE FOCUSED DESIGN
interface IPower { void Supply(); } class Lighthouse { private readonly IPower power; public Lighthouse(IPower power) { this.power = power; } public void Signal() { power.Supply(); } } // Setup: new Lighthouse(new Battery());
Illustrative C# sketches. Supporting types and method bodies are omitted; these snippets are not executed in the page.
06 / MAKE IT STICK
Think of a part of your application that is difficult to change or test. Would this principle address the actual cause? What would become simpler, and what extra structure would it introduce?
Try the question opposite, then continue to the next principle.
CHECK YOUR INSTINCT
SOLID is a set of design guidelines, not a scorecard. The aim is software that stays understandable as real requirements change.