Interface Segregation Principle (ISP)

What is Interface Segregation Principle (ISP) ?

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

ISP primarily focuses on the design of interfaces in a software system.

The Interface Segregation Principle encourages creating interfaces that are tailored to the specific needs of clients or implementing classes, rather than imposing a one-size-fits-all approach.

This approach leads to more maintainable, flexible, and client-specific code, reducing the risk of unintended consequences and facilitating better design and code management.

Key Requirement for implementation of ISP

  1. Granularity: Interfaces should have fine granularity, which means they should contain a small and specific set of methods related to a particular aspect of functionality.
  2. Client-Specific Interfaces: If a class or client needs a set of methods for a particular task, create a separate interface specific to that task. This avoids forcing other clients to implement methods they don't need.
  3. No Empty Methods: Avoid defining empty or default method implementations in interfaces. Implementing classes should only provide method implementations that are meaningful to them.
  4. Adherence to Other SOLID Principles: Applying ISP contributes to adhering to other SOLID principles, such as the Single Responsibility Principle (SRP) and the Dependency Inversion Principle (DIP).

Benefits of ISP

  1. Flexibility: ISP promotes flexibility in your design. As requirements change or new clients need specific interfaces, you can add new interfaces without affecting existing classes.
  2. Code Maintenance: Smaller and focused interfaces are easier to maintain and understand. They also make it clear which classes are responsible for what.
  3. Dependency Management: Implementing classes should depend only on the specific interfaces they use, reducing unnecessary dependencies.

Note. Example of Interface Segregation Principle (ISP) in C# with a simple example using hypothetical scenario involving electronic devices.

Suppose you are designing a system for managing electronic devices, and you have a set of interfaces to represent various device functionalities.

Interface Segregation Violation (Monolithic Interface)

Turn On & Off method are used to switch on off device.

Recharge method to charge battery of device.

// Device action interface that includes both switchable and rechargeable functionalities
public interface IDeviceAction
{
    void TurnOn();
    void TurnOff();
    void Recharge();
}

In above scenario, any class implementing IDeviceAction is forced to provide implementations for both TurnOn and Recharge methods, even if it's a device that doesn't support recharging.

This violates the ISP because classes that only need to work with switchable devices are forced to implement unnecessary methods.

Improved Interface Segregation (Separate Interfaces)

// Interface for switchable devices
public interface ISwitchable
{
    void TurnOn();
    void TurnOff();
}

// Interface for rechargeable devices
public interface IRechargeable
{
    void Recharge();
}

In this improved design, we have separate interfaces for switchable (ISwitchable) and rechargeable (IRechargeable) devices.

Now, implementing classes can choose the interfaces that align with their functionalities.

For example, a light switch class can implement ISwitchable, and a smartphone class can implement both ISwitchable and IRechargeable.

There's no requirement to provide unnecessary method implementations.

Conclusion

By adhering to the Interface Segregation Principle, you achieve a more flexible and maintainable design. Clients and implementing classes are not burdened with unnecessary dependencies or methods, and you can easily extend the system by adding new interfaces for specific functionalities as needed.


Similar Articles