Bird
0
0

You need to add a new payment method 'CryptoPayment' to an existing system using the Payment Strategy Pattern. What is the best approach?

hard📝 Trade-off Q8 of 15
LLD - Design — Online Shopping Cart
You need to add a new payment method 'CryptoPayment' to an existing system using the Payment Strategy Pattern. What is the best approach?
ACreate a new class implementing the PaymentStrategy interface and add it to the context
BModify existing payment classes to include crypto logic
CAdd a new method in PaymentContext for crypto payments
DReplace the PaymentStrategy interface with a new one supporting crypto
Step-by-Step Solution
Solution:
  1. Step 1: Follow open-closed principle

    Adding new payment methods should not modify existing code but extend it.
  2. Step 2: Implement new strategy class

    Create CryptoPayment class implementing PaymentStrategy interface and use it in context without changing existing classes.
  3. Final Answer:

    Create a new class implementing the PaymentStrategy interface and add it to the context -> Option A
  4. Quick Check:

    Extend by new class, don't modify existing code [OK]
Quick Trick: Add new strategy class to extend payment methods [OK]
Common Mistakes:
  • Modifying existing classes breaking open-closed principle
  • Adding methods to context for each payment type
  • Replacing interface causing backward incompatibility

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes