Bird
0
0

How can you combine the Payment Strategy Pattern with a Factory Pattern to improve payment method creation?

hard📝 Trade-off Q9 of 15
LLD - Design — Online Shopping Cart
How can you combine the Payment Strategy Pattern with a Factory Pattern to improve payment method creation?
AUse factory pattern inside each payment strategy class
BReplace strategy pattern with factory pattern entirely
CUse a factory to create payment strategy instances based on input, then pass to context
DUse factory to create context instead of strategies
Step-by-Step Solution
Solution:
  1. Step 1: Understand roles of patterns

    Factory creates objects, strategy defines interchangeable algorithms.
  2. Step 2: Combine patterns properly

    Use factory to instantiate correct payment strategy based on input, then inject into context for execution.
  3. Final Answer:

    Use a factory to create payment strategy instances based on input, then pass to context -> Option C
  4. Quick Check:

    Factory creates strategies, context uses them [OK]
Quick Trick: Factory creates strategies; context uses them [OK]
Common Mistakes:
  • Replacing strategy with factory
  • Putting factory logic inside strategies
  • Using factory to create context instead of strategies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes