Interface Segregation Principle
Clients should not be forced to depend on interfaces they do not use.
This means a class should not be required to implement methods it doesn’t need. ISP encourages splitting large interfaces into more specific ones.
To demonstrate the Interface Segregation Principle (ISP), consider the following example with a Worker interface:
❌ Before Applying ISP
# worker.py class Worker: def work(self): pass def eat(self): pass class HumanWorker(Worker): def work(self): print("Working...") def eat(self): print("Eating lunch...") class RobotWorker(Worker): def work(self): print("Working...") def eat(self): raise NotImplementedError("Robots don't eat")# worker.py class Worker: def work(self): pass def eat(self): pass class HumanWorker(Worker): def work(self): print("Working...") def eat(self): print("Eating lunch...") class RobotWorker(Worker): def work(self): print("Working...") def eat(self): raise NotImplementedError("Robots don't eat")
🔴 Problem:
RobotWorkeris forced to implement theeatmethod even though it doesn’t make sense for robots.
✅ After Applying ISP
# interfaces.py class Workable: def work(self): pass class Eatable: def eat(self): pass# interfaces.py class Workable: def work(self): pass class Eatable: def eat(self): pass
# workers.py class HumanWorker(Workable, Eatable): def work(self): print("Working...") def eat(self): print("Eating lunch...") class RobotWorker(Workable): def work(self): print("Working...")# workers.py class HumanWorker(Workable, Eatable): def work(self): print("Working...") def eat(self): print("Eating lunch...") class RobotWorker(Workable): def work(self): print("Working...")
Now:
HumanWorkerimplements bothWorkableandEatable.RobotWorkeronly implementsWorkable, not forced to define irrelevant behavior.
This design makes the code:
- ✅ Cleaner and more meaningful
- ✅ Easier to maintain and extend
- ✅ More aligned with real-world roles