ISP (Interface Segregation Principle) — Explained with Examples
The Interface Segregation Principle states that no client should be forced to depend on methods it does not use, favoring many specific interfaces over one general one.
ISP stands for Interface Segregation Principle, the fourth of the five SOLID principles. Robert C. Martin defined it: “Clients should not be forced to depend upon interfaces that they do not use.”
Why ISP Matters
Fat interfaces force implementing classes to provide stub or empty implementations for methods they don’t need. This clutters code, violates YAGNI, and creates coupling between unrelated functionality. Segregated interfaces keep each contract focused and minimal.
Real-World Analogy
A restaurant menu shouldn’t list every ingredient in the kitchen. The cook only needs to see the ticket for their station, not the entire order. Segregated interfaces are like giving each station its own focused ticket instead of one massive order slip everyone must read.
Example: Violating vs Following ISP
# Violating ISP — one fat interface
class MultiFunctionPrinter(ABC):
@abstractmethod
def print(self, document): pass
@abstractmethod
def scan(self, document): pass
@abstractmethod
def fax(self, document): pass
class SimplePrinter(MultiFunctionPrinter):
def print(self, document):
print(f"Printing {document}")
def scan(self, document):
pass # forced stub — doesn't support scanning
def fax(self, document):
pass # forced stub — doesn't support faxing
# Following ISP — segregated interfaces
class Printer(ABC):
@abstractmethod
def print(self, document): pass
class Scanner(ABC):
@abstractmethod
def scan(self, document): pass
class Fax(ABC):
@abstractmethod
def fax(self, document): pass
class SimplePrinter(Printer):
def print(self, document):
print(f"Printing {document}")
class MultiFunctionDevice(Printer, Scanner, Fax):
def print(self, document):
print(f"Printing {document}")
def scan(self, document):
print(f"Scanning {document}")
def fax(self, document):
print(f"Faxing {document}")Related Terms
SOLID, Single Responsibility Principle, Separation of Concerns, YAGNI
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro