Design patterns · interview question
Why is it important for a context in the Strategy Pattern to use an interface for strategies?
Multiple choiceEasychapter-13-curatedpremiumdesign-patternsstrategy-pattern
Answer and explanation
Correct answer
B. To allow swapping strategies without modifying the context.
Why
Using an interface allows the context to swap strategies without modifying its code, adhering to the Open/Closed Principle and promoting loose coupling between the context and the strategies.
Test yourself on Design patterns
A free test with instant, explained results. No account needed.
More Design patterns questions
- If an e-commerce platform needs to switch between different inventory management algorithms depending on the region,…
- How would you implement a Proxy pattern to control access to a remote service that calculates tax rates in an…
- Which pattern should you use to provide a consistent API for multiple e-commerce payment providers?
- Which of the following is a key characteristic of the Proxy pattern in Java?
- What should a payment adapter own when integrating a vendor SDK?
- How would you explain a key difference between the Proxy and Adapter patterns in an interview?