Skip to content
LLD Learn/SOLID Principles
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Single Responsibility Principle

4 min read

You'll learn to

  • -State the Single Responsibility Principle as "a class has one reason to change", not "a class does one thing"
  • -Find the people or teams behind each reason to change, and split a class along those lines
  • -Avoid splitting so far that logic that always changes together gets pulled apart

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

The Single Responsibility Principle (SRP) is usually summarized as "a class should do one thing". That is close enough to be useful and vague enough to be misused. Robert C. Martin, who named the principle, gave a sharper version: a class should have only one reason to change.

An Analogy: The Restaurant Where One Person Does Everything

Imagine a tiny restaurant where one person cooks, serves the tables and keeps the accounts. Now the menu changes, the tax rules change and the dining-room layout changes. Every change lands on the same person, and a mistake in one job can wreck another. A bigger restaurant gives each job its own person, so a new tax form does not interrupt the cooking. SRP does the same for classes: one class per reason for change, so a change in one area cannot break another.

"One Reason to Change", Not "One Method"

A class can have five methods and still follow SRP, as long as all five serve the same responsibility. The violation to look for is a class you would have to open for two unrelated reasons. Each reason usually has a stakeholder, a person or team who asks for the change. Consider an Employee class that does three jobs.

  • -Calculating pay changes when Finance changes the rules.
  • -Saving to the database changes when the database team changes storage.
  • -Formatting a printed report changes when whoever owns reports wants a new layout.

Put those in one class and three groups of people are now editing the same file. A harmless tweak to the report layout can break the pay calculation, and the tests for one job must live with the risks of the others.

Splitting by reason to change, not by method count
# Before: two unrelated responsibilities in one class
class Employee:
    def calculate_pay(self) -> float: ...
    def save_to_database(self) -> None: ...
    def generate_report(self) -> str: ...

# After: each class has exactly one reason to change
class Employee:
    def calculate_pay(self) -> float: ...

class EmployeeRepository:
    def save(self, employee: Employee) -> None: ...

class EmployeeReportFormatter:
    def generate(self, employee: Employee) -> str: ...

After the split, a request from Finance touches only Employee, a request from the database team touches only EmployeeRepository, and a request about reports touches only EmployeeReportFormatter. Three kinds of change no longer collide.

The Overcorrection: Splitting for Its Own Sake

People who have just learned SRP often go too far and split every class down to one method. The result technically follows the rule and is practically unreadable: fifteen tiny classes for one coherent idea. The principle is about separating things that change for different reasons. If two pieces of logic always change together because of the same business rule, keeping them in one class is cohesion, which means related things are kept together, and it is a good thing.

  • -Split: calculate_pay and generate_report have different owners and change at different times.
  • -Keep together: an Invoice that has both total() and tax_for(country), because Finance changes both at once when tax law changes.

A useful gut check before you split a class: "Who would ask for this change, and who would ask for that one?" If both come from the same team for the same reason, you may be fragmenting rather than separating real concerns.

Interview Signal

You spot an OrderProcessor class that validates an order, charges the payment, and sends a confirmation email, all in one process() method. Is this an SRP violation?

Weak Answer

"Yes - it has three methods worth of logic, so it should be three classes."

Strong Answer

"Likely yes, but for a specific reason: validation logic changes when business rules change, payment logic changes when we integrate a new payment provider, and email logic changes when marketing wants a new template - three different stakeholders, three different reasons to change. I'd extract OrderValidator, PaymentCharger, and OrderNotifier, and have OrderProcessor orchestrate them rather than implement all three itself."

Check Yourself1 / 5

What is the sharper, more useful formulation of the Single Responsibility Principle?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!