Bird
Raised Fist0

You are designing a payment processing system that must support new payment methods without changing existing code. Which design approach best aligns with the Open/Closed Principle?

easy🔍 Pattern Recognition Q11 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
You are designing a payment processing system that must support new payment methods without changing existing code. Which design approach best aligns with the Open/Closed Principle?
ACreating a new subclass for each payment method that implements a common payment interface
BModifying the existing payment processor class to handle new payment methods directly
CAdding new conditional branches inside the existing payment processor class for each new payment method
DUsing global variables to switch between payment methods at runtime
Step-by-Step Solution
  1. Step 1: Identify extension vs modification

    Adding new payment methods should extend behavior without modifying existing classes.
  2. Step 2: Evaluate subclassing with common interface

    Creating subclasses for each payment method allows extension by adding new classes, adhering to OCP.
  3. Step 3: Why other options fail

    Adding new conditional branches inside the existing payment processor class for each new payment method and modifying existing code violate the 'closed for modification' part. Using global variables to switch between payment methods at runtime uses global state, which is unrelated and error-prone.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Extension via polymorphism, no modification of existing code.
Quick Trick: Extend with new classes, don't modify existing ones [OK]
Common Mistakes:
MISTAKES
  • Thinking adding conditionals is extension
  • Believing modifying existing code is acceptable if tested
  • Confusing global state management with OCP
Trap Explanation:
PITFALL
  • Options A and B seem straightforward but violate the principle by modifying existing code; option D distracts with unrelated global state usage.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to recognize when and how to apply OCP in design.
Master "Open/Closed Principle - Open for Extension, Closed for Modification" 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