2.2.7 · HinglishDesign Principles

SOLID — Interface Segregation Principle

1,703 words8 min readRead in English

2.2.7 · Coding › Design Principles


YE HAI KYA?

Yahan client ka matlab hai wo code jo interface par depend karta hai — caller BHI aur implementer BHI. Jab ek interface unrelated capabilities ko bundle karta hai, toh har implementer un sab mein ghich jaata hai.


YE EXIST KYUN KARTA HAI?

Fat interfaces ke teen concrete pains:

  1. Forced dummy implementations → dead code, type system mein jhooth.
  2. Coupling by accidenteat() ka signature change karo aur har worker recompile hoga, robots bhi.
  3. Misleading contracts — callers trust nahi kar sakte ki koi method actually kaam karti hai.

KAISE APPLY KAREIN

Figure — SOLID — Interface Segregation Principle

Worked Example 1 — Classic Robot


Worked Example 2 — Multifunction Printer


Worked Example 3 — Win measure karna


Common Mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho ek restaurant menu jo tumhe force karta hai soup, salad AUR dessert saath order karne ke liye, chahe tumhe sirf soup chahiye. Annoying hai, na? Ek achha menu tumhe sirf jo chahiye wo pick karne deta hai. Ek interface ek menu hai un cheezeon ka jo ek piece of code kar sakta hai. ISP kehta hai: code ko aisi abilities "order" karne ke liye force mat karo jo kabhi use nahi hogi — use sirf apni dishes wala chhota menu do.


Active Recall

SOLID mein I ka kya matlab hai?
Interface Segregation Principle.
Interface Segregation Principle state karo.
Kisi bhi client ko aisi methods (interface members) par depend karne ke liye force nahi karna chahiye jo wo use nahi karta.
Code mein ISP violation ka core symptom kya hai?
Implementers empty/dummy methods likhte hain ya un methods ke liye UnsupportedOperationException throw karte hain jo wo use nahi karte.
"Role interface" kya hota hai?
Ek chhota interface jo us hisaab se define hota hai ki ek specific client ko kya chahiye, sirf cohesive methods ko group karta hai jo saath use hote hain.
ISP fix: fat interface ko kis criterion se split karein?
Cohesive client role/need se — individual method se NAHI (over-splitting avoid karo).
ISP aur SRP mein kya fark hai?
SRP ek class ke reasons to change ko limit karta hai; ISP limit karta hai ki ek client kya depend karne ke liye force hota hai. ISP "interfaces ke liye SRP" hai.
m methods aur implementer i ke u_i use karne par, fat interface kitni forced dependencies impose karta hai?
i ke over sum of (m - u_i); segregation ise 0 tak reduce karta hai.
Kaunsa doosra SOLID principle violate hota hai jab ek interface method ko exception ke saath stub kiya jaata hai?
Liskov Substitution Principle (subtype supertype ka contract tod deta hai).

Connections

Concept Map

advocates

rejects

causes

causes

causes

analogous to

achieved via

enables

ensures

illustrated by

fixed by

Interface Segregation Principle

Fat interface

Small role interfaces

Client depends only on what it uses

Forced dummy implementations

Accidental coupling

Misleading contracts

Recipe: group methods by role

Class implements multiple small interfaces

SRP for interfaces

RobotWorker example