DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Reasons for adhering to the Interface Segregation Principle (ISP) matter in software design

1. Understanding the Interface Segregation Principle

Image

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. It focuses on creating interfaces that are specific to the needs of clients, preventing the negative impact of having a single interface with multiple methods that some clients do not require.

1.1 What is the Interface Segregation Principle?

Image

The ISP states that no client should be forced to depend on methods it does not use. In other words, an interface should only contain methods that are relevant to the classes implementing it. This principle helps in creating more focused and maintainable code by ensuring that interfaces are not overly complex.

Example:

Consider an interface Animal with methods eat(), fly(), and swim():

public interface Animal {
    void eat();
    void fly();
    void swim();
}
Enter fullscreen mode Exit fullscreen mode

If a class Dog implements this interface, it will be forced to provide implementations for fly() and swim(), even though dogs do not fly or swim:

public class Dog implements Animal {
    @Override
    public void eat() {
        // Dog eating implementation
    }

    @Override
    public void fly() {
        // Not applicable for Dog
    }

    @Override
    public void swim() {
        // Not applicable for Dog
    }
}
Enter fullscreen mode Exit fullscreen mode

1.2 Problems with a Generalized Interface

Having a generalized interface like the one above leads to code that is harder to understand and maintain. The Dog class ends up with unnecessary methods that do not align with its actual behavior. This violates the ISP, as the Dog class is being forced to implement methods it doesn't need.

2. Applying the Interface Segregation Principle

Image

To adhere to the ISP, you should break down large interfaces into smaller, more specific ones. Each interface should be tailored to the needs of the clients that implement it.

2.1 Creating Specific Interfaces

Instead of one large interface, create multiple, smaller interfaces. For example:

public interface Eater {
    void eat();
}

public interface Flyer {
    void fly();
}

public interface Swimmer {
    void swim();
}
Enter fullscreen mode Exit fullscreen mode

Now, you can create classes that implement only the interfaces they need:

public class Dog implements Eater {
    @Override
    public void eat() {
        // Dog eating implementation
    }
}

public class Bird implements Eater, Flyer {
    @Override
    public void eat() {
        // Bird eating implementation
    }

    @Override
    public void fly() {
        // Bird flying implementation
    }
}
Enter fullscreen mode Exit fullscreen mode

2.2 Benefits of Interface Segregation

By following the ISP:

  • Maintainability : Code becomes easier to understand and maintain because each class only implements relevant methods.
  • Flexibility : It’s easier to make changes to one interface without affecting other parts of the code.
  • Scalability : New features can be added with minimal disruption by introducing new interfaces.

Demo Code:

Let’s demonstrate the benefits with a simple example. Assume we have a system where Dog and Bird are used:

public class AnimalDemo {
    public static void main(String[] args) {
        Eater dog = new Dog();
        Eater bird = new Bird();
        Flyer birdFlyer = (Flyer) bird;

        dog.eat(); // Outputs: Dog is eating
        bird.eat(); // Outputs: Bird is eating
        birdFlyer.fly(); // Outputs: Bird is flying
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, each class and interface serve a clear purpose, and the code adheres to the ISP, resulting in a more organized and flexible design.

3. Conclusion

The Interface Segregation Principle is a fundamental aspect of designing clean and maintainable software. By ensuring that interfaces are specific to the needs of their clients, you can avoid unnecessary complexity and improve the overall quality of your code.

If you have any questions or want to discuss how to implement the ISP in your projects, feel free to leave a comment below!

Read posts more at : Reasons for adhering to the Interface Segregation Principle (ISP) matter in software design

Top comments (0)