SOLID & Design Patterns Interview Question #9

How is ISP different from SRP?

SOLID Principles, Design Fundamentals, Repository, Factory, Strategy & Decorator Junior Beginner

Quick Interview Answer

SRP focuses on the responsibility and reasons for change of a class or module. ISP focuses on the contract exposed to clients and prevents them from depending on operations they do not need.

Detailed Explanation

SRP focuses on the responsibility and reasons for change of a class or module. ISP focuses on the contract exposed to clients and prevents them from depending on operations they do not need.

The principles often reinforce each other, but they address different design problems. A class can have one responsibility while still exposing an unnecessarily broad interface.

Code Example

public interface IReadRepository<T>
{
    Task<T?> GetByIdAsync(int id);
}

public interface IWriteRepository<T>
{
    Task AddAsync(T entity);
}