Bird
Raised Fist0

When is approach 2 clearly better than approach 1?

hard⚖️ Approach Comparison Q8 of Q15
OOP & Design Patterns - Strategy Pattern - Replace Conditionals with Polymorphism
Consider two approaches to implementing payment processing: 1. Using conditional statements inside a single class. 2. Using the Strategy Pattern with dependency injection. When is approach 2 clearly better than approach 1?
AWhen performance is critical and conditionals are faster
BWhen the number of payment methods is fixed and never changes
CWhen new payment methods need to be added frequently without modifying existing code
DWhen the system must minimize memory usage
Step-by-Step Solution
Solution:
  1. Step 1: Compare maintainability of approaches

    Conditional approach requires modifying existing code to add new methods, violating the open-closed principle.
  2. Step 2: Identify Strategy Pattern advantage

    Strategy Pattern allows adding new behaviors by creating new classes without changing existing code.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Strategy improves extensibility for frequent additions [OK]
Quick Trick: Strategy excels when behaviors change often [OK]
Common Mistakes:
MISTAKES
  • Picking performance over maintainability
  • Ignoring open-closed principle
Trap Explanation:
PITFALL
  • Candidates confuse performance with maintainability trade-offs.
Interviewer Note:
CONTEXT
  • Tests understanding of design trade-offs between approaches.
Master "Strategy Pattern - Replace Conditionals with Polymorphism" 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