2.2.4Design Principles

SOLID — Single Responsibility Principle

1,926 words9 min readdifficulty · medium4 backlinks

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:

  1. List the methods of the suspect class.
  2. Tag each method with its actor (who requests changes to it?).
  3. Group by actor. Each distinct actor → its own class.
  4. Extract shared data into a passive data structure (no policy).
  5. Optionally provide a Facade so callers still have one entry point.
Figure — SOLID — Single Responsibility Principle

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 AA different actors. A change request from any actor has some chance pp of accidentally affecting behaviour another actor depends on (shared state/helpers).


Flashcards

What single phrase defines SRP?
A class should have one, and only one, reason to change.
"Reason to change" maps to what real-world concept?
An actor — a single stakeholder/role that requests changes.
Why is "a class should do only one thing" a wrong reading of SRP?
SRP counts reasons to change (actors), not number of operations; a class can have many methods serving one actor.
In the Employee god-class, what actors collide?
Accounting (calculatePay), HR/Ops (reportHours), and DBA (save).
What is the typical SRP fix for a god-class?
Extract a passive data struct and give each actor its own class; optionally add a Facade.
What pattern restores a single entry point after over-splitting?
The Facade pattern.
Cross-actor risk formula for AA actors, break prob pp?
R(A)=1(1p)A1R(A)=1-(1-p)^{A-1}.
What is RR when A=1A=1 and why?
00; one actor means no other actor to accidentally break.
Which design smell comes from OVER-applying SRP?
Shotgun surgery / a maze of tiny anaemic classes.
SRP aligns module boundaries with what organisational thing?
The org chart of stakeholders/actors.

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

part of

means

maps to

is a

misread as

causes

violated by

serves

leads to

goal

aligns with

Single Responsibility Principle

SOLID Principles

One Reason to Change

Single Actor

Stakeholder Role

Do Only One Thing

Over-splitting into Tiny Classes

Employee God-class

Two Actors share code

Change breaks other actor

Easy to Change

Org Chart of Stakeholders

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 — R=1(1p)A1R = 1-(1-p)^{A-1} — jo batata hai ki jitne zyada actor ek class me, utna zyada accidentally break hone ka risk. A=1A=1 pe risk zero. Yeh maths SRP ko prove kar deta hai, sirf "best practice" nahi rehta.

Go deeper — visual, from zero

Test yourself — Design Principles

Connections