Phase 8 of 30 · Topic 8.5

Interface Segregation Principle & Interface Bloat Anti-Patterns

1Concept

The Interface Segregation Principle (ISP) dictates that clients should not be forced to depend on interfaces they do not use. Prefer small, focused interfaces (`IReadOnlyList<T>`, `IAsyncDisposable`).

2Architecture Diagram

Anti-Pattern (Fat Monolithic Interface):
[ IBigService ] ──> 50 methods (Forces clients to implement stubs)

Refactored (Clean Segregation):
├── [ IReader ] ──> Read()
├── [ IWriter ] ──> Write()
└── [ IExporter ] ──> Export()

3Code Example

C# 13 & .NET 9
using System;

public interface IEntityReader<T> { T GetById(int id); }
public interface IEntityWriter<T> { void Save(T entity); }

// Small, composable interfaces
public class UserStore : IEntityReader<string>, IEntityWriter<string>
{
    public string GetById(int id) => $"User_{id}";
    public void Save(string entity) => Console.WriteLine($"Saved {entity}");
}

public class IspDemo
{
    public static void ReadOnlyOperation(IEntityReader<string> reader)
    {
        Console.WriteLine($"Fetched: {reader.GetById(42)}");
    }

    public static void Main()
    {
        var store = new UserStore();
        ReadOnlyOperation(store);
    }
}

4Expected Output

Fetched: User_42

5Key Takeaways

  • Keep interfaces single-purpose with 1 to 5 cohesive methods.
  • Combine interfaces via inheritance when higher-level composite contracts are required.
  • Easier to mock and unit test.