SOLID principles
intermediateInterface Segregation Principle (ISP)
Clients should not be forced to depend on methods they do not use.
Do not force a class to implement methods it does not need. That is ISP.
Power tools don't all need the same controls. A drill has trigger + reverse; a vacuum has on/off + suction-level. One universal control panel forces every tool to deal with buttons it doesn't have.
Key Concepts
1
An example. You write one interface for office machines:
2
interface Machine {
void print(Doc d);
void scan(Doc d);
void fax(Doc d);
void staple(Doc d);
}3
Now a basic printer has to implement all four. Three of them cannot do anything, so they throw an
exception or sit empty. Those empty methods are the interface telling you it is really several
interfaces.
4
What it costs you. Every class using Machine gets recompiled when any of the four methods changes,
even a class that only ever prints. Tests have to stub methods they do not care about. And a developer
reading Machine cannot tell which parts matter for their case.
Machine
5
The fix is one interface per capability.
interface Printer { void print(Doc d); }
interface Scanner { void scan(Doc d); }
interface Fax { void fax(Doc d); }6
A basic printer implements one. An all-in-one machine implements all three. A method that only scans takes
a Scanner, so it depends on exactly what it uses.
Scanner
7
Java's own interfaces are the example to copy. Comparable, Runnable, AutoCloseable, Iterable —
each does one thing, and classes combine the ones they need.
ComparableRunnableAutoCloseableIterable
8
This is the same bug as LSP, seen from the other side. A class throwing "unsupported" is breaking LSP.
The reason it has to is usually that the interface asked for too much. Fix the interface and both problems
go away.
9
You can overdo it. If a caller needs six interfaces to get one job done, you have swapped one problem
for another. The test is how clients use the methods: ones that are always used together belong together.
It is not "one method per interface".
10
What the interviewer is probing.1. "What is ISP?" Probing: the idea. Stalls: "Small interfaces are better." Moves up: no class
should depend on methods it does not use, plus what that dependency costs.
11
2. "How do you spot it?" Probing: the practical signal. Stalls: "Big interfaces." Moves up:
empty or throwing methods in implementations, and clients using one method out of ten.
12
3. "How do you split it?" Probing: the criterion. Stalls: "One method each." Moves up: by what
clients actually use together.
13
4. "How does this relate to LSP?" Probing: connecting the ideas. Stalls: "Both are SOLID." *Moves
up:* an unsupported-operation stub breaks LSP, and the fat interface is usually why it exists.
14
5. "Can you split too much?" Probing: balance. Stalls: "No." Moves up: yes — a caller needing
six interfaces for one job is the opposite failure.
15
Fat interface and its stubs16
interface Machine {
void print(Doc d);
void scan(Doc d);
void fax(Doc d);
void staple(Doc d);
}17
class BasicPrinter implements Machine {
public void print(Doc d) { ... }
public void scan(Doc d) { throw new UnsupportedOperation...; }
public void fax(Doc d) { throw new UnsupportedOperation...; }
public void staple(Doc d){ /* silently does nothing */ }
}18
Three of four methods are noise, every client recompiles when
any of them changes, and a caller cannot tell what is real.19
Segregated by role20
interface Printer { void print(Doc d); }
interface Scanner { void scan(Doc d); }
interface Fax { void fax(Doc d); }21
class BasicPrinter implements Printer { ... }
class OfficeMFD implements Printer, Scanner, Fax { ... }22
void archive(Scanner s) // depends on exactly what it uses23
The JDK as the worked example
Comparable, Runnable, Callable, AutoCloseable, Iterable --
single-purpose, composed freely. That composability is the
payoff ISP is describing.24
The balance test
Do clients use these methods TOGETHER?
yes -> keep them in one interface
no -> split along the line that actually divides them
Not "one method per interface".