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

Prefer small, cohesive interfaces to large, "fat" ones.

What Does Interface Mean in ISP?

In the context of ISP the interface of a type is whatever can be accessed by client code working with an instance of that type.

  • C# interface type/keyword.
  • Class features with the public accessor.

What is a Client?

  • It is the calling code.
  • In this context, the client is the code that is interacting with an instance of the interface.

Violating ISP results in classes that depend on things they don't need.

Large interfaces depend on more dependencies. This means:

  • More coupling
  • Brittle code
  • Testing becomes difficult
  • Difficult to deploy

Detecting ISP Violations

  • Larger interfaces
  • NotImplementedException
  • Code uses just a small subset of a larger interface
public class SmtpNotificationService : INotificationService
{
    public void SendEmail(string to,
                          string from,
                          string body)
    {
        // Send email code
    }

    public void SendText(string SMSNumber,
                         string message)
    {
        throw now NotImplementedException();
    }
}
This interface lacks cohesion

We can fix the code in Figure 1 by splitting the interface into cohesive sets. In this case, containing one item each.

public interface IEmailNotificationService
{
    void SendEmail(string to,
                   string from,
                   string subject,
                   string body);
}

public interface ITextNotificationService
{
    void SendText(string SMSNumber,
                  string message);
}
Cohesive Interfaces

What about legacy code that is coupled to the original interface?

We can use multiple interface inheritance.

public interface INotificationService:
                                    IEmailNotificationService,
                                    ITextNotificationService
{
}
Multiple interface inheritance

Releted Concepts

Liskov Substitution Principle

Large interfaces are hard to implement and thus are likely to be partially implemented and therefore not be fully substitutable for their base type.

Cohesion and SRP

Small cohesive interfaces are preferable to large interfaces whose methods a loosely related to one another.

Fxing ISP Violations

  • Break up large interfaces into smaller ones
    • Compose fat interfaces from smaller ones for backward compatibility.
  • To address large interfaces you don't control
    • Create a small, cohesive interface
    • Use the Adapter design pattern so your code can work with the Adapter
  • Clients should own and define their interfaces

Where Do Interfaces Live in Our Apps?

  • Client code should define and own the interfaces it uses.
  • Interfaces should be declared where both client code and implementations can access it.

Learn More

Key Takeaways

  • Prefer small, cohesive interfaces to large, expansive ones.
  • Following ISP helps with SRP and LSP.
  • Break up large interfaces by using
    • Interface inheritance
    • The Adapter design pattern

Other Links

stackify.com/interface-segregation-principle

Copyright © 2025 delaney. All rights reserved.