Bird
Raised Fist0

A class is responsible for both generating reports and sending them via email. Which design pattern best aligns with the Single Responsibility Principle to refactor this class?

easy🔍 Pattern Recognition Q2 of Q15
OOP & Design Patterns - Single Responsibility Principle - One Class, One Reason to Change
A class is responsible for both generating reports and sending them via email. Which design pattern best aligns with the Single Responsibility Principle to refactor this class?
AObserver Pattern to notify email sender after report generation.
BSeparate ReportGenerator and EmailSender classes to isolate responsibilities.
CFactory Pattern to create different report types.
DSingleton Pattern to ensure only one instance handles both tasks.
Step-by-Step Solution
Solution:
  1. Step 1: Identify responsibilities

    Report generation and email sending are distinct reasons to change.
  2. Step 2: Choose design aligning with SRP

    Separating into ReportGenerator and EmailSender classes isolates responsibilities, adhering to SRP.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Separate classes for separate reasons to change [OK]
Quick Trick: Separate classes for different change reasons [OK]
Common Mistakes:
MISTAKES
  • Confusing design patterns with SRP enforcement
  • Using patterns that don't isolate responsibilities
Trap Explanation:
PITFALL
  • Candidates often pick patterns unrelated to responsibility separation, missing SRP focus.
Interviewer Note:
CONTEXT
  • Checks ability to map SRP to design pattern choices.
Master "Single Responsibility Principle - One Class, One Reason to Change" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes