Skip to the guide

Interface segregation: small, focused interfaces

IntermediateConcept6 pages

The interface segregation principle says that no class should be forced to depend on methods it doesn't use. In practice, you give each kind of caller a small interface with only the methods it calls, instead of one large interface for everything. Interviewers want to see you spot a "fat" interface, split it the right way, and stop splitting before it becomes silly.

A universal remote

Think of a universal remote for a living room. It has buttons for every device in the room: power, brightness for the lights, and lock and unlock for the front door. Whatever you point it at receives every button. Point it at a plain lamp and press "lock", and the lamp's light blinks to say "not supported".

A grandmother who only switches her reading lamp on, off and brighter finds the big remote confusing. So her family gives her a small remote with just those buttons. The small remote works only with lamps that dim, so a "dim" from it can never reach something that can't dim.