Interface Segregation Principle
You'll learn to
- -State the Interface Segregation Principle: no class should be forced to depend on methods it does not use
- -Spot a fat interface by looking for implementers that fake or throw from some methods
- -Split an interface by capability without cutting it into pointless one-method pieces
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
The Interface Segregation Principle (ISP) says no class should be forced to depend on methods it does not use. A single "fat" interface that bundles unrelated abilities forces every implementer to either supply methods that make no sense for it or to throw errors from them. That is the interface-level cousin of the Liskov Substitution problems from the previous chapter.
An Analogy: The Job Contract With Everything In It
Imagine a contract that says "you will cook, drive the delivery van, repair the oven and do the accounts", and you were hired only to cook. You would sign for three duties you cannot perform. ISP says to write a smaller contract for each duty, and let people sign only the ones that apply to them.
The Classic Fat-Interface Trap
A Worker interface with work() and eat() looks reasonable for a human worker. A robot worker has no use for eat(), but the interface forces it to supply one, usually as an empty body or an error. The fix is to split the contract into Workable and Eatable. Humans implement both and robots implement only the one that applies.
# Before: RobotWorker is forced to implement eat(), which makes no sense
class Worker(ABC):
@abstractmethod
def work(self) -> None: ...
@abstractmethod
def eat(self) -> None: ...
# After: capabilities are separate, implemented only where they apply
class Workable(ABC):
@abstractmethod
def work(self) -> None: ...
class Eatable(ABC):
@abstractmethod
def eat(self) -> None: ...
class HumanWorker(Workable, Eatable): ...
class RobotWorker(Workable): ... # never forced to implement eat()The demo below shows the same trap with printers. First, one fat contract forces an inkjet printer to promise scanning it cannot do, and the mistake only shows up as a crash when someone calls it. Then the contract is split, and the mistake becomes visible before the program runs.
With the split contracts, a type-checking tool would flag copy_machine(SimpleInkjet()) before the program ever runs, because a simple inkjet is not a Scanner. The mistake moved from a crash in production to an error at your desk.
Do Not Fragment Past Usefulness
ISP has the same overcorrection risk as SRP. Splitting an interface into one method per interface "just in case" produces a maze of tiny types. The right size groups methods that the same kind of client always needs together. A Readable with read() and close() makes sense as one interface if every reader always needs both. Splitting them would be fragmentation without any client that wants only one.
A fast ISP test: take each class that implements an interface and ask, "Does it have a real, working body for every method?" Any method it fakes or throws from is a candidate for splitting out.
A Printer interface has print(), scan(), and fax(). A SimpleInkjetPrinter can only print. How do you handle this?
"Have SimpleInkjetPrinter implement Printer and just throw NotSupportedException from scan() and fax()."
"That's exactly the ISP violation - SimpleInkjetPrinter would be forced to depend on methods it can't honor. I'd split Printer into separate Printable, Scannable, and Faxable interfaces, and have SimpleInkjetPrinter implement only Printable. A MultiFunctionPrinter can implement all three without any class ever faking a capability it doesn't have."
What does the Interface Segregation Principle state?
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!