2.2.7Design Principles

SOLID — Interface Segregation Principle

1,774 words8 min readdifficulty · medium2 backlinks

WHAT is it?

The word client here means the code that depends on the interface — both the caller AND the implementer. When an interface bundles unrelated capabilities, every implementer is dragged into all of them.


WHY does it exist?

Three concrete pains of fat interfaces:

  1. Forced dummy implementations → dead code, lies in the type system.
  2. Coupling by accident — change eat()'s signature and every worker recompiles, even robots.
  3. Misleading contracts — callers can't trust that a method actually works.

HOW to apply it

Figure — SOLID — Interface Segregation Principle

Worked Example 1 — The classic Robot


Worked Example 2 — Multifunction Printer


Worked Example 3 — Measuring the win


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a restaurant menu that forces you to order soup, salad AND dessert together, even if you only want soup. Annoying, right? A good menu lets you pick just what you want. An interface is a menu of things a piece of code can do. ISP says: don't force code to "order" abilities it will never use — give it a small menu with only its dishes.


Active Recall

What does the I in SOLID stand for?
Interface Segregation Principle.
State the Interface Segregation Principle.
No client should be forced to depend on methods (interface members) it does not use.
What is the core symptom of an ISP violation in code?
Implementers writing empty/dummy methods or throwing UnsupportedOperationException for methods they don't use.
What is a "role interface"?
A small interface defined by what a specific client needs, grouping only cohesive methods used together.
ISP fix: split a fat interface by what criterion?
By cohesive client role/need — NOT by individual method (avoid over-splitting).
How does ISP differ from SRP?
SRP limits a class's reasons to change; ISP limits what a client is forced to depend on. ISP is "SRP for interfaces."
For m methods and implementer i using u_i, how many forced dependencies does the fat interface impose?
Sum over i of (m - u_i); segregation reduces this to 0.
Which other SOLID principle is violated by stubbing an interface method with an exception?
Liskov Substitution Principle (the subtype breaks the supertype's contract).

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

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ISP ka matlab simple hai: kisi bhi class ko aise methods implement karne ke liye force mat karo jo wo use hi nahi karti. Agar tum ek bada "fat" interface banate ho — jaise Machine jisme print(), scan(), fax() sab ek saath hai — to jo printer sirf print karta hai usko bhi scan() aur fax() ka khaali (dummy) implementation likhna padega, ya UnsupportedOperationException throw karna padega. Yeh ek tarah ka jhooth hai, kyunki type bol raha hai "main scan kar sakta hoon" jabki actually nahi kar sakta.

Solution? Bade interface ko chhote role interfaces mein tod do — Printer, Scanner, Fax alag. Ab jo class jo role chahiye usko implement kare. Java mein ek class multiple interfaces implement kar sakti hai, to AllInOne teeno le lega, aur OldPrinter sirf Printer. Compile time pe hi galti pakdi jaati hai — koi galti se robot ko eat() call nahi kar sakta agar robot Feedable implement hi nahi karta.

Ek important baat: ISP ka matlab yeh nahi ki har method ka alag interface bana do. Cohesion dekho — jo methods hamesha saath use hote hain (jaise read aur write ek stream mein), unhe saath rakho. Warna interface ka explosion ho jaata hai aur code samajhna mushkil. Yaad rakho: ISP basically SRP ka interface version hai — har interface ki ek hi wajah honi chahiye change hone ki. Yeh matter karta hai kyunki kam coupling matlab kam bugs, aur change karna easy.

Go deeper — visual, from zero

Test yourself — Design Principles

Connections