What is the Single Responsibility Principle
You'll learn: The foundational SOLID principle that keeps classes focused and maintainable by giving each one a single job.
The Core Idea
The Single Responsibility Principle (SRP) states that a class should have only one reason to change—meaning it should have one primary responsibility or job. When a class tries to do too many things, changes in one area force modifications throughout, creating fragile code that's hard to maintain.
Think of it like a restaurant: the chef cooks, the server takes orders, and the cashier handles payments. Each person has one clear responsibility. If the chef also had to handle payments and take orders, any change to the payment system would disrupt cooking, and menu changes would affect the cash register. That's messy.
Why It Matters
SRP directly builds on high cohesion (things that belong together) and helps reduce coupling (dependencies between components). When a class has multiple responsibilities, it becomes tightly coupled to different parts of the system. A change in business logic might break report generation if both live in the same class.
One reason to change means one source of requirements. If your UserManager class handles both user authentication and email notifications, you have two reasons to change it: security policy updates or email template changes. That violates SRP.
The Payoff
Classes following SRP are:
- Easier to understand (one clear purpose)
- Easier to test (focused behavior)
- More reusable (single-purpose components)
- Less fragile (changes stay localized)
Key Takeaway: A class should do one thing well and have only one reason to change—this keeps your design flexible, focused, and resilient to future modifications.