SOLID — Single Responsibility Principle
WHAT is the Single Responsibility Principle?
The "S" in SOLID:
- S — Single Responsibility ← this note
- O — Open/Closed
- L — Liskov Substitution
- I — Interface Segregation
- D — Dependency Inversion
WHY does SRP matter? (the cost of ignoring it)
When one class serves two actors, a change requested by actor A can accidentally break behaviour relied on by actor B — because they share code.
HOW to apply SRP (the procedure)
Recall Forecast first (cover the answer)
Before reading: How would YOU split the Employee class? What's the recurring pattern?
Answer: Separate the data from the policies, giving each actor its own class.
Keep the shared EmployeeData; create PayCalculator, HourReporter, EmployeeRepository.
Step-by-step refactor:
- List the methods of the suspect class.
- Tag each method with its actor (who requests changes to it?).
- Group by actor. Each distinct actor → its own class.
- Extract shared data into a passive data structure (no policy).
- Optionally provide a Facade so callers still have one entry point.

A way to quantify the coupling (so you can prove it)
We can put a number on "how risky is a change." Let a class be touched by different actors. A change request from any actor has some chance of accidentally affecting behaviour another actor depends on (shared state/helpers).
Flashcards
What single phrase defines SRP?
"Reason to change" maps to what real-world concept?
Why is "a class should do only one thing" a wrong reading of SRP?
In the Employee god-class, what actors collide?
calculatePay), HR/Ops (reportHours), and DBA (save).What is the typical SRP fix for a god-class?
What pattern restores a single entry point after over-splitting?
Cross-actor risk formula for actors, break prob ?
What is when and why?
Which design smell comes from OVER-applying SRP?
SRP aligns module boundaries with what organisational thing?
Recall Feynman: explain to a 12-year-old
Imagine one toy robot that can cook dinner, drive a car, AND do homework. If you ask the robot to cook spicy food, you tweak its "cook" wires — but those wires are tangled with the "drive" wires, so suddenly the car won't start! That's annoying. SRP says: give each job to its own robot. The cooking robot, the driving robot, the homework robot. Now when you change cooking, the car still works fine. One robot = one job = one reason someone tells it to change.
Connections
- Open-Closed Principle — once responsibilities are split, you extend behaviour without editing.
- Dependency Inversion Principle — separated responsibilities talk through abstractions.
- Cohesion and Coupling — SRP is high cohesion (one reason) + low coupling between actors.
- Facade Pattern — tames the extra classes SRP produces.
- God Object (anti-pattern) — the canonical SRP violation.
- Shotgun Surgery — the smell from over-splitting (SRP misapplied).
- DRY Principle — caution: blind DRY can re-couple two actors via a shared helper.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, Single Responsibility Principle ka matlab simple hai: ek class ke paas sirf ek hi reason hona chahiye change hone ka. Yahan "reason to change" ka matlab hai ek actor — yaani ek stakeholder ya role jo aapse change maangta hai. Jaise Accounting ek actor hai, HR doosra actor, aur DBA teesra. Agar ek hi class teeno ko serve kar rahi hai, toh dikkat yeh hai ki jab Accounting koi change maangega, toh shared code ki wajah se HR ka report chupke se badal sakta hai — bina HR ke kuch maange! Yeh bug nahi, design ki galti hai.
Bahut log galti karte hain ki "single responsibility" ka matlab samajhte hain "class sirf ek hi kaam kare, ek hi method ho." Yeh galat hai. Ek class ke 10 methods ho sakte hain aur phir bhi SRP follow kar sakti hai — bas un sabko ek hi actor serve karna chahiye. Asli test yeh hai: "is method ko change kaun maangega?" Agar do alag-alag log/department ek hi class ko pull kar rahe hain, tabhi SRP toot-ta hai.
Fix kaise karein? God-class ke methods ki list banao, har method ke saamne uska actor likho,
phir actor ke hisaab se groups banao. Har group ko apni alag class do, aur shared data ko ek
passive EmployeeData jaisi struct me daal do. Agar bahut saari choti classes ban jaayein,
toh ek Facade laga do taaki bahar se ek hi simple entry point dikhe. Maine note me ek
chhota formula bhi diya — — jo batata hai ki jitne zyada actor ek class
me, utna zyada accidentally break hone ka risk. pe risk zero. Yeh maths SRP ko prove
kar deta hai, sirf "best practice" nahi rehta.