Card 08/ 08

RecapDifficulty: Intermediate1 min

Recap: Interface Segregation

Shape each interface by the code that calls it, and stop splitting when no client uses a piece alone.

The interface segregation principle keeps interfaces as small as their clients need.

  • No class should depend on methods it does not use. That covers implementers forced to write methods and callers that depend on methods they never call.
  • The signs are stubs and defensive callers. A method that raises or does nothing, and a try around a call that should be safe.
  • Split by client, not by method. List the callers first, give each group of calls an interface, and combine pieces where one caller needs several.
  • Splitting changes declarations, not logic. One class can still implement every piece, as the Xerox Job did.
  • Small interfaces are in the standard library. Python's Sized, Iterable and Container combine into Collection.
  • One method per interface is usually too far. Split only where some client uses a piece on its own.