Skip to content
DiaGO

Single Responsibility Principle

A class should have one reason to change. SRP is about aligning each module with a single actor or concern — and it is the most misapplied principle in SOLID.

Open this example in Studio

What SRP actually says

The Single Responsibility Principle is usually quoted as "a class should do one thing." The original formulation is sharper and more useful:

A class should have one reason to change.

"Reason to change" means a distinct source of change requests — a stakeholder, a policy, a format, a technology. A class that computes payroll and formats the payroll report has two reasons to change: the finance rules and the report layout. Those changes come from different people at different times, so bundling them means every layout tweak risks breaking a tax calculation.

Why the principle exists

Mixing responsibilities creates two concrete costs:

  1. Fragility. Unrelated changes travel through the same code, so an edit for one concern can silently break another.
  2. Merge friction and re-testing. Every change forces re-verification of behavior that had nothing to do with the change.

SRP is not about making classes tiny. It is about making change local: when the report format changes, exactly one class should need editing.

A worked example

The classic violation — one class serving three masters:

class EmployeeService {
  calculatePay(employee: Employee): Money { /* finance rules */ }
  saveEmployee(employee: Employee): void { /* database schema */ }
  formatTimesheetReport(employee: Employee): string { /* HR report layout */ }
}

Finance, DBAs, and HR each own one of these methods. Splitting by reason-to-change:

class PayCalculator {
  calculatePay(employee: Employee): Money { /* … */ }
}
 
class EmployeeRepository {
  save(employee: Employee): void { /* … */ }
}
 
class TimesheetReportFormatter {
  format(employee: Employee): string { /* … */ }
}

Each class now changes for exactly one reason, can be tested in isolation, and can be owned by the team that owns that concern.

How it looks in UML

In a class diagram, SRP violations often show up as one class with associations pointing at unrelated corners of the model — persistence types, formatting types, and domain types all at once. After refactoring, each new class clusters with its own collaborators. Open the Studio example to see the before/after side by side.

Reach for it

  • Different stakeholders keep asking for changes to the same class
  • You cannot describe the class's job without the word “and”
  • Tests for one behavior keep breaking when an unrelated behavior changes

Avoid it

  • Splitting classes so far that a single change now touches five files
  • Creating one-method classes for concerns that always change together
  • Applying it speculatively before any real change pressure exists

Common mistakes

  • Equating SRP with "small". The unit is reasons to change, not size.
  • Over-splitting. If two behaviors always change together, they are one responsibility — separating them adds indirection without adding safety.
  • Stopping at classes. SRP applies to functions, modules, and services too. A microservice that owns both billing and notifications has the same disease at a bigger scale.
  • Ignoring cohesion. SRP and High Cohesion are two views of the same goal; a class stripped of its natural data and behavior is less cohesive, not more compliant.

Trade-offs, honestly

Splitting a class adds files, names, and wiring. On small, stable code the cost can exceed the benefit — heuristic principles are guides, not laws. The reliable signal is change history: real, repeated edits arriving from different directions justify the split; imagined future edits usually do not.

Practice

Take a LibraryManager class that checks out books, calculates fines, prints receipts, and writes to the database. List each method's reason to change and regroup them into classes. Then open the Studio example and restructure the diagram to match your split.