liveBook · Manning

Interface Segregation Principle

As explained in Grokking Software Architecture

The Interface Segregation Principle states that a class should not be required to depend on or implement interface methods it does not use.

An interface should be a focused toolbox, not a junk drawer. The Interface Segregation Principle (ISP) says a class should not have to implement methods it does not use. Keep each interface specific to the role its clients need. section 3.4.4

Think of soccer practice: a midfielder needs shooting and tackling drills, not goalie-specific diving saves. In software, forcing a midfielder class to provide unused goalie methods creates useless code, often with errors thrown from methods that should never be called. A bloated interface also couples clients to changes in methods they do not use. section 3.4.4 section 3.4.4

Listing 3.9 “Before”: Violating ISP - “Fat Interface”
class Midfielder {
    practiceShooting() { /* ... */ }
    practiceTackling() { /* ... */ }
    practiceDivingSaves() { #A
        throw new Error("practiceDivingSaves() is not implemented."); #B
    }
    practiceHandDistribution() { #A
        throw new Error("practiceHandDistribution() is not implemented."); #B
    }
}

The bloated version gives Midfielder goalie methods that only throw errors. section 3.4.4

Listing 3.10 “After”: Following ISP
class Midfielder {
    practiceShooting() { /* ... */ }
    practiceTackling() { /* ... */ }
}
 
class Goalie {
    practiceShooting() { /* ... */ }
    practiceTackling() { /* ... */ }
    practiceDivingSaves() { /* ... */ }
    practiceHandDistribution() { /* ... */ }
}

The focused version gives Midfielder and Goalie methods suited to their roles. section 3.4.4

Use ISP to keep related methods together and let each client depend only on what it needs. The aim is not small interfaces for their own sake, but less confusion and less coupling when code changes. section 3.4.4

How Grokking Software Architecture covers it

How deep it goes
Defines the principle, illustrates before-and-after code, and explains interface bloat and unnecessary coupling.
Examples
Uses soccer practice roles and Midfielder and Goalie classes to contrast bloated and focused interfaces.
What you can do after
Evaluate interfaces by checking whether clients must depend on or implement methods they do not use.
How it connects
Connects interface segregation to Separation of Concerns, high cohesion, and loose coupling.

Also explained in

Compare how 4 books explain Interface Segregation Principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.