A single interface forces every implementor to support methods they do not need. How do you fix that without breaking callers?
You split the fat interface into several small ones, and you keep the old one working by having it extend the small ones. So the callers do not have to change.
public class Main {
public static void main(String[] args) {
SimplePrinter p = new SimplePrinter();
p.print("hi");
}
}
And here is the fat interface before the change.
// The fat interface. Every implementor has to have all
// of these, even the ones it does not need.
public interface Printer {
void print(String text);
void print(String text, boolean bold);
void scan();
void fax(String number);
void copy(int copies);
}
And here are the small ones that replace it.
public interface Printable {
void print(String text);
}
public interface BoldPrintable extends Printable {
void print(String text, boolean bold);
}
public interface Scannable {
void scan();
}
public interface Copyable {
void copy(int copies);
}
And now the old interface, changed so that it extends the small ones. That is the part that keeps the callers working.
// The old interface still exists and it still has all
// the old methods, so every old caller still compiles.
public interface Printer extends BoldPrintable, Scannable, Copyable {
// It is empty. The methods come from the small interfaces.
}
And now the classes implement only what they need.
public class SimplePrinter implements Printable {
@Override
public void print(String text) {
System.out.println(text);
}
}
public class MultiFunctionPrinter implements BoldPrintable, Scannable, Copyable {
@Override
public void print(String text) {
System.out.println(text);
}
@Override
public void print(String text, boolean bold) {
System.out.println(bold ? text.toUpperCase() : text);
}
@Override
public void scan() {
System.out.println("scanning");
}
@Override
public void copy(int copies) {
System.out.println("copying " + copies);
}
}
And the four steps that do it without breaking anything.
the step what you do
-------------------------------- --------------------------------------------
1. write the small interfaces the new, correct shape
2. make the old interface extend the old interface keeps every
them method it had
3. move the implementations each class implements only what
it needs
4. change the callers to hold one at a time, and the old
the small interface interface can stay for now
And the reason the old interface can stay is that it has no methods of its own any more. It only says that its implementations are also Printable, and Scannable and so on. So anything that held a Printer still works and still has all the same methods.
And there is one more thing that is worth knowing about, and it is the question of what to do with the old interface in the end. You keep it while the old callers exist, and you delete it when they have all moved over. If it is published in a library then you may have to keep it for ever, and that is a cost that you paid by making the interface fat in the first place.
So the rule to remember is this. Split the interface and make the old one extend the small ones so that the callers keep working.
The next two questions in this topic explain the principle and the case where the class really needs several capabilities.