AlgoViz

Interface segregation (ISP)

medium 50 min

No client should be forced to depend on methods it does not use.

Watch lesson4:23

Where it shows up

The same pattern appears across everyday software.

  • Go: package io

    `Reader` and `Writer` have one method each. Larger ones such as ReadWriter are built from them. Effective Go says that interfaces with one or two methods are common in Go code.

  • Go: interfaces live with the client

    The Go code review guide says that interfaces generally belong in the package that uses the values, and that the implementing package should return concrete types. New methods can then be added without breaking anyone.

  • Go: net/http

    A response writer has three methods. Being able to flush is a second interface, `Flusher`. `ResponseController` looks for an `Unwrap` method, so that wrappers do not hide such abilities.

  • Java: MouseListener and MouseAdapter

    Five methods in one listener. The abstract class MouseAdapter has empty bodies and, in the words of its documentation, exists as convenience. A patch for a fat interface, in the same package.

  • Java: Executor and ExecutorService

    `Executor` has one method, `execute`. `ExecutorService` adds shutting down and futures. A class that only submits work can ask for the small one and cannot stop the pool.

  • JDBC: Wrapper

    `unwrap(Class)` returns an object that implements the given interface, to reach methods that a proxy does not expose. A standard answer to abilities lost behind wrappers.

  • Android: GestureDetector

    `SimpleOnGestureListener` implements all methods of the gesture listener and returns false, so that an app overrides only the gestures it cares about.

  • .NET: IReadOnlyList

    A read-only collection by index, without Add or Remove. Because it only gives elements out, it can be covariant: `IReadOnlyList<out T>`.

  • Python: collections.abc

    `Sized`, `Iterable` and `Container` have one method each. `Collection` combines them, `Sequence` builds on it, and `MutableSequence` adds the writing half.

  • Spring Data

    A repository can extend CrudRepository with everything, or the empty `Repository` and list only the methods to expose. The documentation shows a base interface with just `findById` and `save`.

  • Docker plugins

    On activation a plugin answers with the list of subsystems it implements, for example VolumeDriver. Docker then sends only calls for these.