2.2.4 · HinglishDesign Principles

SOLID — Single Responsibility Principle

1,756 words8 min readRead in English

2.2.4 · Coding › Design Principles


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? (isse ignore karne ki keemat)

Jab ek class do actors serve karti hai, toh actor A ki requested change accidentally actor B ke behaviour ko break kar sakti hai — kyunki woh code share karte hain.


HOW to apply SRP (procedure kya hai)

Recall Pehle sochho (answer cover karo)

Padhne se pehle: Tum Employee class ko KHUD kaise split karoge? Recurring pattern kya hai?

Answer: Data ko policies se alag karo, har actor ko uski apni class do. Shared EmployeeData rakho; PayCalculator, HourReporter, EmployeeRepository banao.

Step-by-step refactor:

  1. Suspect class ke methods list karo.
  2. Har method ko uske actor ke saath tag karo (changes kaun request karta hai?).
  3. Actor ke hisaab se group karo. Har distinct actor → uski apni class.
  4. Shared data ko ek passive data structure mein extract karo (koi policy nahi).
  5. Optionally ek Facade provide karo taaki callers ke paas abhi bhi ek entry point rahe.
Figure — SOLID — Single Responsibility Principle

Coupling ko quantify karne ka ek tarika (taaki tum prove kar sako)

Hum "change kitni risky hai" pe ek number daal sakte hain. Maano ek class alag-alag actors ke baare mein jaanti hai. Kisi bhi actor se ek change request ka kuch chance hai ki accidentally aise behaviour par asar ho jo doosra actor depend karta hai (shared state/helpers).


Flashcards

SRP define karne wala ek phrase kya hai?
Ek class ka ek, aur sirf ek, reason to change hona chahiye.
"Reason to change" kis real-world concept se map hota hai?
Ek actor se — ek akela stakeholder/role jo changes request karta hai.
"Ek class ko sirf ek kaam karna chahiye" SRP ki galat reading kyun hai?
SRP reasons to change (actors) count karta hai, na ki operations ki ginti; ek class ke paas bahut saare methods ho sakte hain jo ek actor serve karte hain.
Employee god-class mein kaun se actors clash karte hain?
Accounting (calculatePay), HR/Ops (reportHours), aur DBA (save).
God-class ke liye typical SRP fix kya hai?
Ek passive data struct extract karo aur har actor ko uski apni class do; optionally Facade add karo.
Over-splitting ke baad ek single entry point restore karne wala pattern kaunsa hai?
Facade pattern.
actors aur break prob ke liye cross-actor risk formula kya hai?
.
pe kya hai aur kyun?
; ek actor ka matlab hai koi doosra actor nahi jo accidentally break ho sake.
SRP ko OVER-apply karne se kaun sa design smell aata hai?
Shotgun surgery / tiny anaemic classes ka chakravyuh.
SRP module boundaries ko kis organisational cheez ke saath align karta hai?
Stakeholders/actors ke org chart ke saath.

Recall Feynman: 12-saal ke bacche ko samjhao

Socho ek toy robot jo khana bana sakta hai, car chala sakta hai, AUR homework kar sakta hai. Agar tum robot ko spicy khana banana kaho, tum uski "cook" wires tweak karte ho — lekin woh wires "drive" wires ke saath tangled hain, toh suddenly car start nahi hogi! Yeh irritating hai. SRP kehta hai: har kaam ke liye uska apna robot do. Cooking robot, driving robot, homework robot. Ab jab tum cooking change karo, car theek se chalti rehti hai. Ek robot = ek kaam = ek reason ki koi usse change karne ko kahe.


Connections

  • Open-Closed Principle — jab responsibilities split ho jaati hain, tum behaviour ko bina edit kiye extend kar sakte ho.
  • Dependency Inversion Principle — separated responsibilities abstractions ke through baat karte hain.
  • Cohesion and Coupling — SRP yaani high cohesion (ek reason) + actors ke beech low coupling.
  • Facade Pattern — SRP ke produce kiye extra classes ko sambhalta hai.
  • God Object (anti-pattern) — canonical SRP violation.
  • Shotgun Surgery — over-splitting ka smell (SRP misapplied).
  • DRY Principle — savdhaan: blind DRY do actors ko ek shared helper ke zariye re-couple kar sakta hai.

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