Bird
Raised Fist0
LLDsystem_design~25 mins

Single Responsibility Principle in LLD - System Design Exercise

Choose your learning style10 modes available
Design: Single Responsibility Principle Application
Focus on designing a system architecture that follows the Single Responsibility Principle (SRP). Do not cover other SOLID principles or detailed implementation code. Exclude performance optimization and deployment concerns.
Functional Requirements
FR1: Design a system where each module or class has only one reason to change.
FR2: Ensure that each component handles a single responsibility or functionality.
FR3: Allow easy maintenance and scalability by minimizing the impact of changes.
FR4: Support clear separation of concerns within the system.
Non-Functional Requirements
NFR1: The system should be modular and easy to understand.
NFR2: Changes in one responsibility should not affect others.
NFR3: The design should support future extensions without modifying existing modules.
NFR4: The system should be simple enough for beginners to understand.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
Key Components
Modules or classes representing single responsibilities
Interfaces or abstractions to separate concerns
Communication between modules with minimal coupling
Error handling and logging separated from business logic
Design Patterns
Single Responsibility Principle (SRP)
Separation of Concerns
Modular Design
Interface Segregation
Reference Architecture
  +-------------------+      +-------------------+      +-------------------+
  |   User Interface  | ---> |  Business Logic   | ---> |   Data Access     |
  |  (Handles input)  |      | (Processes data)  |      | (Reads/Writes DB) |
  +-------------------+      +-------------------+      +-------------------+
          |                         |                          |
          |                         |                          |
          +-------------------------+--------------------------+
                            Clear separation of responsibilities
Components
User Interface Module
Any UI framework or plain code
Handles user input and output only, no business logic
Business Logic Module
Any programming language classes or modules
Processes data and applies business rules, no UI or data storage code
Data Access Module
Database connectors or ORM
Handles all database interactions, no business logic or UI code
Request Flow
1. User interacts with the User Interface module.
2. User Interface sends input data to the Business Logic module.
3. Business Logic processes data and applies rules.
4. Business Logic requests data storage or retrieval from Data Access module.
5. Data Access module interacts with the database and returns results.
6. Business Logic sends processed results back to User Interface.
7. User Interface displays results to the user.
Database Schema
Not applicable as SRP focuses on code/module design rather than database design.
Scaling Discussion
Bottlenecks
Modules becoming too large and handling multiple responsibilities.
Tight coupling between modules making changes risky.
Difficulty in testing due to mixed responsibilities.
Slow development and maintenance due to unclear module boundaries.
Solutions
Refactor large modules into smaller ones each with a single responsibility.
Use interfaces and abstractions to reduce coupling.
Write unit tests for each module independently.
Regularly review and enforce SRP during code reviews.
Interview Tips
Time: Spend 10 minutes explaining SRP concept and importance, 20 minutes designing a simple system applying SRP, 15 minutes discussing scaling and maintenance benefits.
Explain SRP as one reason to change per module/class.
Show clear separation of concerns in your design.
Discuss how SRP improves maintainability and scalability.
Mention how SRP helps in testing and reduces bugs.
Highlight the importance of modular design and loose coupling.

Practice

(1/5)
1. What does the Single Responsibility Principle (SRP) mean in simple terms?
easy
A. A class should handle multiple tasks to reduce the number of classes.
B. Each class or module should have only one job or responsibility.
C. All functions should be written in a single class for simplicity.
D. Classes should share responsibilities to improve collaboration.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Each class or module should have only one job or responsibility. -> Option B
  4. Quick Check:

    Single Responsibility Principle = One job per class [OK]
Hint: One class, one job keeps code clean and simple [OK]
Common Mistakes:
  • Thinking one class should do many tasks
  • Believing all code should be in one place
  • Confusing SRP with code reuse
2. Which of the following class names best follows the Single Responsibility Principle?
easy
A. UserAuthenticator
B. DataProcessorAndReportGenerator
C. UserManagerAndLogger
D. FileHandlerAndNetworkSender

Solution

  1. Step 1: Analyze class names for single responsibility

    Classes with multiple responsibilities in their names violate SRP. UserManagerAndLogger, DataProcessorAndReportGenerator, and FileHandlerAndNetworkSender suggest multiple jobs.
  2. Step 2: Identify the class with one clear responsibility

    UserAuthenticator clearly focuses on one task: authenticating users, which aligns with SRP.
  3. Final Answer:

    UserAuthenticator -> Option A
  4. Quick Check:

    Class name with one job = UserAuthenticator [OK]
Hint: Class name shows one clear job means SRP followed [OK]
Common Mistakes:
  • Choosing classes with multiple jobs in name
  • Ignoring SRP and mixing responsibilities
  • Assuming longer names are better
3. Given this Python class, what is the main SRP violation?
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
medium
A. The class handles order validation, saving, and email sending, mixing responsibilities.
B. The class does not validate the order properly.
C. The class should not have any methods.
D. The class only processes orders and follows SRP.

Solution

  1. Step 1: Identify responsibilities in the class

    The class OrderProcessor validates orders, saves orders, and sends confirmation emails. These are three distinct jobs.
  2. Step 2: Check SRP compliance

    SRP requires one responsibility per class. Mixing validation, saving, and emailing violates SRP.
  3. Final Answer:

    The class handles order validation, saving, and email sending, mixing responsibilities. -> Option A
  4. Quick Check:

    Multiple jobs in one class = SRP violation [OK]
Hint: One class, one job; multiple jobs mean SRP broken [OK]
Common Mistakes:
  • Ignoring multiple responsibilities in one class
  • Thinking multiple methods mean multiple classes
  • Assuming SRP allows all related tasks in one class
4. You have a class that both calculates taxes and generates invoices. What is the best way to fix this SRP violation?
medium
A. Combine tax calculation and invoice generation into a single method.
B. Add more methods to handle discounts and payments in the same class.
C. Split the class into two: one for tax calculation and one for invoice generation.
D. Remove tax calculation and keep only invoice generation.

Solution

  1. Step 1: Identify the problem with the current class

    The class does two jobs: calculating taxes and generating invoices, which breaks SRP.
  2. Step 2: Apply SRP by separating responsibilities

    Splitting into two classes, each with one responsibility, fixes the violation and improves maintainability.
  3. Final Answer:

    Split the class into two: one for tax calculation and one for invoice generation. -> Option C
  4. Quick Check:

    Separate classes for separate jobs = SRP fix [OK]
Hint: Split mixed jobs into separate classes to follow SRP [OK]
Common Mistakes:
  • Adding unrelated methods to the same class
  • Combining jobs into one method
  • Removing needed functionality instead of splitting
5. You are designing a system for a library. Which design best follows the Single Responsibility Principle?
hard
A. A single class 'LibrarySystem' that manages books, users, loans, and notifications.
B. A single class 'Library' that handles everything including maintenance and reporting.
C. One class 'LibraryManager' that handles books and users, and another class 'LoanAndNotificationManager' for loans and alerts.
D. Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts. -> Option D
  4. Quick Check:

    Separate classes for separate jobs = SRP followed [OK]
Hint: One class per job in system design follows SRP best [OK]
Common Mistakes:
  • Grouping unrelated responsibilities in one class
  • Mixing multiple jobs in fewer classes
  • Ignoring SRP for convenience
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard