SOLID & Design Patterns Interview Question #2

What is the Single Responsibility Principle (SRP)?

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

Quick Interview Answer

The Single Responsibility Principle states that a class or module should have one reason to change. In practice, this means grouping behavior around one coherent responsibility instead of making one class handle unrelated concerns.

Detailed Explanation

The Single Responsibility Principle states that a class or module should have one reason to change. In practice, this means grouping behavior around one coherent responsibility instead of making one class handle unrelated concerns.

For example, an OrderService should not also generate PDF invoices, send emails, and write audit files if those responsibilities can change independently.

Code Example

public class OrderService
{
    private readonly IOrderRepository _repository;

    public OrderService(IOrderRepository repository)
    {
        _repository = repository;
    }

    public Task SaveAsync(Order order)
        => _repository.SaveAsync(order);
}