SOLID: S — Single Responsibility Principle

A class should have one reason to change: one job.

Why it matters

When one class does several jobs, unrelated changes collide: a change to the email wording risks breaking the tax calculation, and every change means retesting everything.

The idea

If one class holds the business rules, the database saving and the email sending, a change to any one of them risks breaking the others. Split it so each class does one thing well and changes for one reason.

A restaurant where the chef cooks, the cashier takes payments and the cleaner cleans. If the chef also did the accounts, every new tax rule would interrupt the cooking.

In a tiny café one person does everything, and that's fine. Splitting too early into very small classes adds complexity; apply this when responsibilities really do change separately.

How it works

  1. List what the class does, and who asks for changes to each part (finance, the database team, marketing).
  2. Each different 'who' is a separate responsibility.
  3. Move each responsibility into its own class with a clear name.
  4. Connect them in a small coordinating function or service.

Example

Invoice calculates the totals, InvoiceRepository saves it, InvoiceEmailer sends it.

Key points

Common mistakes

Not true: Single responsibility: one method per class.
In fact: It means one reason to change: one person or team who would ask for changes. A class can have many methods that serve that one purpose.

In the interview

Single responsibility means each class has one reason to change. For example, I'd separate invoice calculation, saving and emailing, so a change to the email template can't break the tax logic.

Part of Unit 5: Low-level design: OOP and SOLID in the free System Design course.