2.2.5Design Principles

SOLID — Open - Closed Principle

1,698 words8 min readdifficulty · medium

WHY does OCP exist?


WHAT exactly is "open" and "closed"?


HOW to actually achieve it (the mechanism)

The core enablers are polymorphism and the Dependency Inversion habit:

  1. Find the part of the code that varies (the thing that keeps making you edit a switch/if-else).
  2. Hide that variation behind an abstract interface.
  3. Make high-level code depend on the interface, not the concrete types.
  4. New requirement → write a new class implementing the interface. Old code never opens.
Figure — SOLID — Open - Closed Principle

Worked Example 1 — Area calculator (the classic smell)


Worked Example 2 — Discount strategies


Common Mistakes (Steel-manned)


Flashcards

OCP stands for and states what
Open/Closed Principle — software entities should be open for extension but closed for modification.
What does "open for extension" mean
You can add new behavior to the module as requirements grow.
What does "closed for modification" mean
You add that behavior without editing the existing source code.
The key mechanism that makes OCP possible
Abstraction + polymorphism — depend on a stable interface, add new implementations behind it.
The design-pattern most associated with OCP
Strategy pattern (and Template Method / plugin architectures).
A long if/elif or switch on a "type" field is a smell for which violated principle
Open/Closed Principle.
The "Rule of Three" tells you to
Refactor to polymorphism on the third variation, not before (avoid premature abstraction).
Does OCP forbid bug fixes
No — it targets adding new behavior; fixing bugs still requires editing code.
Which SOLID letter is OCP
The "O".
What goes inside a new Shape class vs the AreaCalculator
The shape's own area formula goes in the new class; the calculator only calls the area() abstraction.

Recall Feynman: explain to a 12-year-old

Imagine you have a toy robot that can do tricks. Right now it can wave and spin. You want it to also dance. Bad way: open up the robot and rewire its brain — you might break the waving. OCP way: the robot has slots where you can plug in trick cards. To add dancing, you just slide in a new "dance card." The robot's insides never get opened. Old tricks stay safe, new tricks get added. That's "open for new tricks, closed for cutting it open."


Connections

Concept Map

means

means

motivates

achieved by

resolved via

resolved via

enabled by

guided by

hidden behind

done by

implements

demonstrates

Open Closed Principle

Open for extension

Closed for modification

Editing tested code risks regression

Add new code not edit old

Depend on stable interface

Polymorphism

Dependency Inversion habit

Isolate the varying part

New requirement adds new class

Area calculator example

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Open/Closed Principle ka matlab simple hai: code ko naya behavior add karne ke liye open hona chahiye, lekin purana tested code edit karne ke liye band (closed) hona chahiye. Soch ke dekho — jab bhi aap ek already-working file ko kholke if/else me ek aur branch ghusate ho, to risk hai ki kuch jo pehle chal raha tha woh toot jaye. OCP kehta hai: edit mat karo, naya class add karo.

Yeh kaise hota hai? Jadoo ki cheez hai abstraction aur polymorphism. Aap ek interface banate ho (jaise Shape jisme area() hai). Aapka main code sirf us interface pe depend karta hai, concrete classes pe nahi. Ab naya shape chahiye? Bas Triangle(Shape) likh do — AreaCalculator ko haath lagane ki zarurat hi nahi. Yahi Strategy pattern ka core idea hai.

Ek practical tip: har jagah interface mat banao "future ke liye" — woh over-engineering ho jata hai. Rule of Three yaad rakho: pehli-doosri baar if/else chalega, lekin teesri variation aaye to refactor karke polymorphism la do. Aur dhyan rakho — OCP bug fix karne se mana nahi karta; woh sirf naye features ke liye purana code chhedne se rokta hai. Yaad rakhne ka mantra: "Add, don't edit" — purana code closed, naya code plug-in.

Go deeper — visual, from zero

Test yourself — Design Principles

Connections