Repaso
Español

The Single Responsibility Principle says a class should…

Options

  1. Have only one method
  2. Have only one reason to change (Correct option)
  3. Be used by only one other class
  4. Never inherit

Answer

Have only one reason to change

Why

SRP (the S in SOLID) means a class should do one job, so only one kind of change request should ever force you to edit it.

Example: a class that calculates an invoice AND saves it to the database AND sends the email changes when tax rules change, when the database changes and when the email template changes. Split it into three classes, each with one job.

Remember

One class, one job, one reason to change.

Key terms

Practice this question in Repaso