Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) is one of the SOLID principles of software design. Its main idea is:

“A class should not be forced to implement methods it does not use.”

Simple Explanation:

Imagine you have a remote control that works for multiple devices like TVs, air conditioners, and music systems. If you’re using it for the TV, you shouldn’t care about buttons for setting the temperature. Similarly, in software, a class should only have the methods it actually needs.

How to Solve This?

Instead of creating one big interface with many methods, we create several smaller, more specific interfaces.

Example:

Bad Example (Violating ISP):

A single interface has methods for all devices:

from abc import ABC, abstractmethod

class Device(ABC):
    @abstractmethod
    def turn_on(self):
        pass

    @abstractmethod
    def turn_off(self):
        pass

    @abstractmethod
    def set_volume(self, volume):
        pass

    @abstractmethod
    def set_temperature(self, temperature):
        pass

If we create a TV class, it will have to implement all methods, even set_temperature, which makes no sense for a TV:

class TV(Device):
    def turn_on(self):
        print("TV is turned on.")

    def turn_off(self):
        print("TV is turned off.")

    def set_volume(self, volume):
        print(f"TV volume set to {volume}.")

    def set_temperature(self, temperature):
        # This method is irrelevant for a TV
        raise NotImplementedError("TV cannot set temperature.")

This design forces the TV to implement unnecessary methods.

Good Example (Following ISP):

We split the large interface into smaller, focused ones:

class PowerDevice(ABC):
    @abstractmethod
    def turn_on(self):
        pass

    @abstractmethod
    def turn_off(self):
        pass


class VolumeControl(ABC):
    @abstractmethod
    def set_volume(self, volume):
        pass


class TemperatureControl(ABC):
    @abstractmethod
    def set_temperature(self, temperature):
        pass

Now, the TV class only implements the interfaces it actually uses:

class TV(PowerDevice, VolumeControl):
    def turn_on(self):
        print("TV is turned on.")

    def turn_off(self):
        print("TV is turned off.")

    def set_volume(self, volume):
        print(f"TV volume set to {volume}.")

An Air Conditioner class implements different interfaces based on its needs:

class AirConditioner(PowerDevice, TemperatureControl):
    def turn_on(self):
        print("Air conditioner is turned on.")

    def turn_off(self):
        print("Air conditioner is turned off.")

    def set_temperature(self, temperature):
        print(f"Air conditioner temperature set to {temperature}°C.")

Benefits of ISP:

  1. Simpler code – Classes only deal with methods they need.
  2. Less error-prone – No unnecessary or irrelevant methods.
  3. Easier to extend – New devices only implement the required interfaces.

About the author

Vili M, PhD

With an extensive experience in programming, Vili has dedicated his career to developing innovative solutions and advancing technology. As an expert in programming, electromagnetic fields, robotics, and teaching skills, he combines academic knowledge with practical expertise to deliver impactful results.