What if fixing one bug never broke another part of your system again?
Why Single Responsibility Principle in LLD? - Purpose & Use Cases
Imagine you are building a simple app where one class handles user data, processes payments, and sends emails all at once.
When something breaks, you have to dig through a big mess of code to find the problem.
Doing everything in one place makes the code confusing and hard to fix.
Changing one part can accidentally break another.
It slows down development and causes bugs.
The Single Responsibility Principle says each part should do only one job.
This keeps code clean, easy to understand, and safe to change.
class UserManager {
void saveUser() { /* save user */ }
void processPayment() { /* payment logic */ }
void sendEmail() { /* email logic */ }
}class UserRepository { void saveUser() { /* save user */ } } class PaymentProcessor { void processPayment() { /* payment logic */ } } class EmailSender { void sendEmail() { /* email logic */ } }
It enables building systems that are easier to maintain, test, and grow without fear of breaking unrelated parts.
Think of a restaurant kitchen where one chef only makes desserts, another only grills meat, and another handles salads.
This way, each chef masters their task and the kitchen runs smoothly.
Each module or class should have one clear responsibility.
Separating concerns reduces bugs and speeds up changes.
Clean design leads to easier teamwork and better software.
Practice
Solution
Step 1: Understand the principle's core idea
The Single Responsibility Principle states that a class or module should have one and only one reason to change, meaning it should do one job.Step 2: Match the explanation to the options
Each class or module should have only one job or responsibility. clearly states that each class or module should have only one job, which matches the principle.Final Answer:
Each class or module should have only one job or responsibility. -> Option BQuick Check:
Single Responsibility Principle = One job per class [OK]
- Thinking one class should do many tasks
- Believing all code should be in one place
- Confusing SRP with code reuse
Solution
Step 1: Analyze class names for single responsibility
Classes with multiple responsibilities in their names violate SRP. UserManagerAndLogger, DataProcessorAndReportGenerator, and FileHandlerAndNetworkSender suggest multiple jobs.Step 2: Identify the class with one clear responsibility
UserAuthenticator clearly focuses on one task: authenticating users, which aligns with SRP.Final Answer:
UserAuthenticator -> Option AQuick Check:
Class name with one job = UserAuthenticator [OK]
- Choosing classes with multiple jobs in name
- Ignoring SRP and mixing responsibilities
- Assuming longer names are better
class OrderProcessor:
def process_order(self, order):
self.validate_order(order)
self.save_order(order)
self.send_confirmation_email(order)
def validate_order(self, order):
pass
def save_order(self, order):
pass
def send_confirmation_email(self, order):
pass
Solution
Step 1: Identify responsibilities in the class
The class OrderProcessor validates orders, saves orders, and sends confirmation emails. These are three distinct jobs.Step 2: Check SRP compliance
SRP requires one responsibility per class. Mixing validation, saving, and emailing violates SRP.Final Answer:
The class handles order validation, saving, and email sending, mixing responsibilities. -> Option AQuick Check:
Multiple jobs in one class = SRP violation [OK]
- Ignoring multiple responsibilities in one class
- Thinking multiple methods mean multiple classes
- Assuming SRP allows all related tasks in one class
Solution
Step 1: Identify the problem with the current class
The class does two jobs: calculating taxes and generating invoices, which breaks SRP.Step 2: Apply SRP by separating responsibilities
Splitting into two classes, each with one responsibility, fixes the violation and improves maintainability.Final Answer:
Split the class into two: one for tax calculation and one for invoice generation. -> Option CQuick Check:
Separate classes for separate jobs = SRP fix [OK]
- Adding unrelated methods to the same class
- Combining jobs into one method
- Removing needed functionality instead of splitting
Solution
Step 1: Analyze each design for responsibility separation
Designs with a single class handling books, users, loans, and notifications, or a single class handling everything, put many jobs into one class, violating SRP. Designs with one class for books and users, and another for loans and notifications, mix multiple jobs in each class.Step 2: Identify the design with clear single responsibilities
Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts. separates concerns into distinct classes, each with one responsibility, following SRP best.Final Answer:
Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts. -> Option DQuick Check:
Separate classes for separate jobs = SRP followed [OK]
- Grouping unrelated responsibilities in one class
- Mixing multiple jobs in fewer classes
- Ignoring SRP for convenience
Ready to prove it?
Pass the test and this pattern moves unknown
