DEV Community

Omar Faruque Shamim
Omar Faruque Shamim

Posted on

Interface Segregation Principle (ISP)

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

This means, interfaces should be small and specific, containing only methods that are relevant to the client. This avoids the problem of having a single large interface with methods that may not be applicable to all its clients.

Why Use the Interface Segregation Principle?

  • Avoids Code Bloat: Large interfaces force clients to implement methods they don’t need, leading to unnecessary complexity and maintenance challenges.

  • Enhances Flexibility: Smaller, focused interfaces are easier to use, modify, and extend without impacting unrelated parts of the code.

  • Improves Code Readability and Maintenance: Segregated interfaces are easier to understand because they focus on specific behaviors.

  • Reduces Coupling: Ensures that classes are dependent only on the functionality they need, making the system more modular and robust to change.

  • Prevents Unused Implementations: Classes implementing a large interface with irrelevant methods often include placeholder or empty implementations, which can confuse developers and lead to potential bugs.

How to Implement the Interface Segregation Principle?

  • Identify Responsibilities: Break down large, multi-purpose interfaces into smaller, specific ones based on distinct behaviors.

  • Design Focused Interfaces: Each interface should define methods related to a single responsibility or closely related functionality.

  • Use Composition Over Inheritance: Compose classes using multiple smaller interfaces rather than inheriting from a large interface.


How to Implement ISP

Without ISP (Violating the Principle):
Here’s an example of a booking system where an INotification interface is used to handle different types of notifications.

public interface INotification
{
    void SendEmail(string message);
    void SendSMS(string message);
    void SendPushNotification(string message);
}

public class EmailNotification : INotification
{
    public void SendEmail(string message)
    {
        Console.WriteLine("Email sent: " + message);
    }

    public void SendSMS(string message)
    {
        throw new NotImplementedException(); // Not needed
    }

    public void SendPushNotification(string message)
    {
        throw new NotImplementedException(); // Not needed
    }
}
Enter fullscreen mode Exit fullscreen mode

Problems:
EmailNotification has to implement methods it doesn’t use (SendSMS and SendPushNotification).
Adding new notification types or methods will affect all classes implementing INotification, even if they don’t use them.

With ISP (Following the Principle):
Refactor the INotification interface into smaller, specific interfaces.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay