Bird
Raised Fist0

Which design approach best addresses this requirement?

easy🔍 Pattern Recognition Q11 of Q15
OOP & Design Patterns - Strategy Pattern - Replace Conditionals with Polymorphism
You have a payment processing system that currently uses multiple if-else statements to handle different payment methods like credit card, UPI, and net banking. The system needs to be extended frequently with new payment methods without modifying existing code. Which design approach best addresses this requirement?
AUse a brute force approach with nested <code>if-else</code> statements for each payment method.
BImplement a strategy pattern where each payment method is encapsulated in its own class implementing a common interface.
CUse a recursive function that selects payment methods based on input parameters.
DApply a greedy algorithm to select the payment method with the lowest processing fee.
Step-by-Step Solution
  1. Step 1: Understand the problem of frequent extension

    The system requires adding new payment methods without modifying existing code, which violates the open-closed principle if using if-else chains.
  2. Step 2: Identify the design pattern that encapsulates behaviors

    The strategy pattern encapsulates each payment method in its own class implementing a common interface, allowing easy extension by adding new classes without changing existing code.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Strategy pattern replaces conditionals with polymorphism [OK]
Quick Trick: Replacing conditionals with polymorphism enables easy extension [OK]
Common Mistakes:
MISTAKES
  • Thinking recursion or greedy algorithms solve extensibility here
Trap Explanation:
PITFALL
  • Greedy or recursion might seem plausible but don't address extensibility or maintainability.
Interviewer Note:
CONTEXT
  • Tests candidate's understanding of design patterns for extensibility and maintainability.
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