link: SOLID

Interface Segregation Principle

Overview

Clients should not be forced to depend on interfaces they do not use.

The Interface Segregation Principle (ISP) suggests creating interfaces that are specific and focused, avoiding what’s called “interface pollution.” Instead of one big interface, it’s better to have several smaller ones that suit different needs. When you follow ISP, your software design becomes tidier and easier to work with. It means you can maintain and add new features more smoothly.

Abstract

ISP emphasizes the importance of making interfaces to client needs, thereby avoiding the compulsion for clients to implement irrelevant interface methods. This leads to a codebase that’s easier to navigate and update, with minimal side effects from changes.

Content

Principle Explained

ISP directs us to craft interfaces that are:

Important

  • Narrow and Focused: Interfaces should be specific to client requirements, ensuring that classes only implement what they need.
  • Client-Centric: Design interfaces with the client’s use in mind, providing them with the functionality they require without the burden of unused methods.
  • Granular and Decoupled: Segregate large, “fat” interfaces into smaller, more coherent ones to reduce the impact of changes and improve adaptability.

Practical Example

To illustrate the Interface Segregation Principle (ISP), let’s examine a library intended to facilitate integration with various cloud services. We’ll see how applying ISP can refine our approach to designing interfaces for these services.

Warning

As with the other principles, you can go too far with this one. Don’t further divide an interface which is already quite specific. Remember that the more interfaces you create, the more complex your code becomes. Keep the balance

Summary

Summary

The Interface Segregation Principle plays a vital role in building software architectures that are flexible, easy to maintain, and capable of scaling up. When interfaces are designed to be specific and focused on what clients need, developers can build systems that can handle changes well and meet the requirements of different clients. This principle is at the core of object-oriented design, delivering on its promise of creating software that’s adaptable and user-centered.