Interface Segregation Principle
The interface segregation principle states that clients should not be forced to implement interfaces or methods they do not use.
More specifically, the ISP suggests that software developers should break down large interfaces into smaller, more specific ones, so that clients only need to depend on the interfaces that are relevant to them.
This can make the codebase easier to maintain.
This principle is fairly similar to the single responsibility principle (SRP).
But it’s not just about a single interface doing only one thing – it’s about breaking the whole codebase into multiple interfaces or components.
Think about this as the same thing you do while working with frontend frameworks and libraries like React, Svelte, and Vue.
You usually break down the codebase into components you only bring in when needed.
This means you create individual components that have functionality specific to them.
The component responsible for implementing scroll to the top, for example, will not be the one to switch between light and dark, and so on.
Here’s an example of code that violates the interface segregation principle:
The code above violates the interface segregation principle because the Fish
class doesn’t need the fly
method. A fish cannot fly.
Birds can’t swim too, so the Bird
class doesn’t need the swim
method.
This is how I fixed the code to conform to the interface segregation principle:
Last updated