Part 3 of 6 · 2 min read
A fat interface
Empty methods are the symptom.
A single interface has been written for everything that a place can do.
interface Place {
void book(int hours);
String describe();
void clean();
void unlock();
}
A Room does all four. A Lobby cannot be booked, so it writes book as an empty method, or one that throws. A scheduler that needs only book still depends on clean and unlock, so any change to those methods reaches it. A menu that needs only describe has the same problem.
Two signs show this: classes with empty or throwing methods (module 3 showed the risk), and callers that use a small part of a big interface.
The split follows the callers. Ask who uses each method, then group methods by user.
interface Bookable { void book(int hours); }
interface Describable { String describe(); }
class Room implements Bookable, Describable {
int booked;
public void book(int h) { booked += h; }
public String describe() { return "Room, booked " + booked; }
}
class Lobby implements Describable {
public String describe() { return "Lobby"; }
}
class Scheduler { void reserve(Bookable b) { b.book(2); } }
class Menu { String show(Describable d) { return d.describe(); } }
Booking a room for 2 hours, then showing it, prints "Room, booked 2". Showing the lobby prints "Lobby". The Lobby has no empty method. The Scheduler cannot touch describe, and the Menu cannot book anything. Each class sees only what it needs, and a class can implement several small interfaces when it truly does several jobs.
Fat interface
A fat interface is an interface with many methods, where most callers use only a few.
Example: the Place interface above, used by a scheduler that needs only book.
Common mistake
Splitting into one-method interfaces for every class without looking at users. Group methods by the caller that needs them together, not mechanically.
Check yourself: 1. What are two signs of a fat interface? 2. How should methods be grouped when splitting? 3. Can one class implement two small interfaces?