Skip to main content

🎯 The SRP Rule

“A class should have only ONE reason to change.”
Think of it like workers in a restaurant:
  • 👨‍🍳 Chef - Only cooks food
  • 🍽️ Waiter - Only serves customers
  • 💰 Cashier - Only handles payments
  • 🧹 Cleaner - Only cleans tables
If the chef also had to serve, clean, AND handle money, one bad day could mess up EVERYTHING!
Simple Rule: If you describe your class and use the word “AND”, you might be breaking SRP!❌ “This class saves users AND sends emails AND logs activity” ✅ “This class saves users”
The hospital analogy: In a hospital, a surgeon operates, a pharmacist dispenses medication, and a radiologist reads scans. They all work on the same patient, but each has a distinct responsibility. If the surgeon also had to compound medications, a change in pharmaceutical regulations would force the surgeon to retrain — even though it has nothing to do with surgery. SRP works the same way: when one class has multiple responsibilities, a change in one area forces modifications that ripple into unrelated areas.

🚨 Spotting SRP Violations

❌ BAD: The God Class


✅ GOOD: Focused Classes


🎮 Real Example: Invoice System

Let’s fix an invoice system step by step:

❌ Before: One Class Does Everything

✅ After: Each Class Has One Job


🧪 How to Check for SRP Violations

Use these questions:
  1. Can you describe the class in one sentence WITHOUT using “and”?
    • ✅ “This class stores user data”
    • ❌ “This class stores user data AND sends emails”
  2. If you need to change one feature, do you touch multiple unrelated methods?
    • ✅ Changing email format only touches EmailService
    • ❌ Changing email format requires editing UserManager
  3. How many reasons could this class change?
    • ✅ One reason (e.g., business rules for orders)
    • ❌ Multiple reasons (email format, database schema, logging format…)
  4. Could you easily test this class in isolation?
    • ✅ UserRepository can be tested with just a mock database
    • ❌ UserManager needs mock database, mock email server, mock logger…
  5. Would a team member understand the class purpose immediately?
    • ✅ “InvoicePrinter” - obviously prints invoices
    • ❌ “InvoiceManager” - does it manage? print? email? save?

💡 Common SRP Violations & Fixes


🏋️ Practice Exercise

This class violates SRP. Split it into focused classes:

📝 Key Takeaways

One Job Only

Each class should have only ONE reason to change

No 'AND' Description

If you use “and” to describe it, split it up

Easy to Test

Focused classes are easy to test in isolation

Easy to Name

Good names describe exactly what the class does

Interview Insight

SRP in LLD interviews: When you present a class diagram, interviewers mentally check if each class has a single, clear responsibility. The most common mistake candidates make is creating a “God class” — a BookingManager that handles reservation logic, payment processing, email notifications, and database persistence all in one. Instead, separate these into ReservationService, PaymentService, NotificationService, and BookingRepository. When the interviewer asks “what happens if we need to switch from email to push notifications?”, you can say “only the NotificationService changes.” That one sentence demonstrates SRP understanding. The rule of thumb: if a class name ends in “Manager” or “Handler” and has more than 5-6 methods, it probably violates SRP.

🏃 Next: Open/Closed Principle

Now that your classes are focused, let’s learn how to add features WITHOUT changing existing code!

Continue to Open/Closed Principle →

Learn how to extend your code without modifying it!