How is the Strategy pattern specifically a form of the Open/Closed Principle?
The Open/Closed Principle says code should be open for extension but closed for modification, and Strategy delivers exactly that for swappable behavior. Once a context class delegates to an interface like PaymentStrategy instead of branching on a type string internally, adding a brand-new algorithm — a new payment method, a new routing option, a new validation rule — means writing one new class that implements the shared interface. The context class itself, along with everything that already depends on it and everything that's already been tested against it, does not change by a single line. That's the concrete, checkable proof of the principle in action: extension happens by addition, not by editing code that already works.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response