THE SOLID HARBOUR · CHAPTER 2 OF 5
Design a stable part of the system so a chosen kind of new behaviour can be added through an extension.
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
Bertrand Meyer introduced the open/closed principle in his 1988 book Object-Oriented Software Construction. Robert C. Martin later discussed how abstractions can protect selected parts of a design from recurring changes.
02 / WHY IT MATTERS
Useful for payment methods, export formats, discount policies and delivery strategies when new variants are a realistic requirement.
An extension point can contain the effect of a new variant. Existing code still needs suitable regression tests, but the central algorithm need not gain another special case.
Repeated edits to a growing conditional can entangle unrelated variants. Adding a ticket type may accidentally alter an existing price or leave a branch unhandled.
KNOW THE TRADE-OFF
Closed does not mean never fix bugs or edit code. Choose which variation to support; no design is closed to every possible change. One simple conditional may be the best starting point.
Next, step into the story and see this problem through a familiar situation.
03 / ONCE UPON A CHANGE
The festival ticket desk has one long instruction sheet: if adult, do this; if child, do that. Every new ticket type sends someone back into the desk’s core rules. A family pass is coming next. Must the desk be rebuilt again?
Keep the desk and give it interchangeable price cards. A new card supplies a price in the agreed way, so the desk can keep doing its job.
Turn to “Try it” to change the design and see what happens.
THE HARBOUR FESTIVAL
04 / PULL THE PAPER TAB
Every new pass changes the desk’s conditional.
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
decimal Price(string pass) { if (pass == "adult") return 20m; if (pass == "child") return 8m; // Each new pass adds another branch here. throw new ArgumentException("Unknown pass"); }
A MORE FOCUSED DESIGN
interface IPriceRule { decimal Price(); } class FamilyPass : IPriceRule { public decimal Price() => 45m; } decimal Sell(IPriceRule rule) => rule.Price(); // Setup still selects or registers the new rule.
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.