Interface Segregation Principle

The Interface Segregation Principle is a guideline for designing interfaces (like classes or methods) in a way that makes them more specialized and easier to use. It says that instead of having one big interface with lots of different methods, it’s better to break it up into smaller interfaces, each with a specific set of methods. This way, a client (like another class or program) that wants to use the interface only has to know about and use the methods it needs, and doesn’t have to be aware of the others. This makes the code more flexible and easier to understand.

Let’s consider an example


public interface AutoMobile {
    public abstract void startEngine();

    public abstract void stopEngine();

    public abstract void changeGear();

    public abstract void accelerate();

    public abstract void brake();

    public abstract void turnOnAirConditioner();

    public abstract void turnOffAirConditioner();
}

class ElectricCar implements AutoMobile {

    @Override
    public void startEngine() {
    }

    @Override
    public void stopEngine() {
      throw new UnsupportedOperationException();
    }

    @Override
    public void changeGear() {

       throw new UnsupportedOperationException();
    }

    @Override
    public void accelerate() {
    }

    @Override
    public void brake() {
    }

    @Override
    public void turnOnAirConditioner() {
    }

    @Override
    public void turnOffAirConditioner() {
    }
}

In the above example, we have a class called Automobile that represents a car. The class has many methods, such as startEngine(), stopEngine(), changeGear(), accelerate(), brake(), turnOnAirConditioner(), turnOffAirConditioner().

Now let’s say we have another class ElectricCar that extends Automobile class, but it does not have a traditional engine and does not have changeGear() and stopEngine() methods.

It will be problematic for ElectricCar class to implement all the methods of Automobile class, as it will have to implement methods which it does not use. This will also confuse the developers who are going to use this class. In this case, we have to throw an exception explicitly to specify that the operation is not supported. So the dead code spreads like “name-of-some-extremely-dangerous-disease”!

We can do is to segregate the controls , promote SRP and OCP. Check the below example:

interface EngineControls {
    public void startEngine();
    public void stopEngine();
}

interface GearControls {
    public void changeGear();
}

interface ClimateControls {
    public void turnOnAirConditioner();
    public void turnOffAirConditioner();
}

class Automobile implements EngineControls, GearControls, ClimateControls {
    public void startEngine() {}
    public void stopEngine() {}
    public void changeGear() {}
    public void turnOnAirConditioner() {}
    public void turnOffAirConditioner() {}
}

class ElectricCar implements EngineControls, ClimateControls {
    public void startEngine() {}
    public void stopEngine() {}
    public void turnOnAirConditioner() {}
    public void turnOffAirConditioner() {}
}

Here we have created multiple interfaces like EngineControls, GearControls, ClimateControls with the respective methods. Automobile class will implement all three interfaces and ElectricCar class will implement only ClimateControls and EngineControls. This way we are segregating the interface and making it more specific to the client’s need.

Also, we can have better reuse as now EngineControls can have implementations say MyRaspberryPiEngine or MySuperHumanRobot. ClimateControls is not restricted to Car it can used in Home, Restaurants and what not!

To summarize, ISP offers the following advantages:

There are several advantages to using the Interface Segregation Principle:

  1. Reduced complexity: By breaking interfaces into smaller, more specific interfaces, the overall complexity of the code is reduced, making it easier to understand and maintain.
  2. Increased flexibility: Clients are only required to implement the methods they need, which allows them to be more flexible and adaptable to change.
  3. Improved reusability: Interfaces that are more specific and focused on a single purpose are more likely to be reusable in other parts of the codebase.
  4. Easier to test: When interfaces are smaller and more focused, it is easier to write unit tests for them, as you only need to test the methods that are relevant to that interface.
  5. Reduced dependencies: By breaking interfaces into smaller, more specific interfaces, the dependencies between different parts of the codebase are reduced, which can make the code more robust and less prone to bugs.
  6. Easy to scale: When interfaces are smaller, more specialized and maintainable, it is easier to add new functionality and scale the codebase without introducing new bugs.

Overall, the Interface Segregation Principle helps to create a cleaner, more maintainable, and scalable codebase.

Thanks for reading!


Posted

in