← All Quizzes
What is the Strategy pattern and how does it relate to the Open/Closed Principle?
Explanation
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it. Strategy achieves the Open/Closed Principle: new strategies can be added without modifying existing code. Common uses: sorting (different comparators), payment processing (different payment methods), compression (different algorithms), and validation rules. Strategy eliminates large conditional statements by replacing them with polymorphic behavior, improving maintainability and testability.