Strategy Pattern
Introduction
A client can select an algorithm from a set of algorithms at runtime.
You know that an object can have states and behaviors. And some of these behaviors may vary among the objects of a class. This pattern focuses on the changing behaviors that can be associated with an object at a specific time.
You also have a context object that controls the logic of setting and evaluating the current strategy.
GOF definition
It defines a family of algorithms, encapsulates each one, and makes them interchangeable. The Strategy pattern lets the algorithm vary independently from the clients that use it.
Java Example
- The interface
java.util.comparator- One can implement this interface and provide different algorithms to do different comparisons using
compare()method. - The Comparison result can be further used in various sorting techniques.
- The
Comparatorinterface plays the role of a strategy interface in this context.
- One can implement this interface and provide different algorithms to do different comparisons using
When to use ?
- Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime.
- Use the pattern to isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.
Implementation
- In the
Strategypattern, you typically have aninterfaceorabstractclass that represents thestrategy, defining a contract for the behavior. Concrete classes implement this interface or extend the abstract class, providing their own implementation for the behavior. The client code holds a reference to the strategy interface and can dynamically set or switch between different concrete strategies as needed.
Example
The
PaymentStrategyinterface defines a contract for payment strategies, with apaymethod that accepts the payment amount.We then create two concrete payment strategy classes:
CreditCardStrategyandPayPalStrategy. These classes implement the PaymentStrategy interface and provide their own implementation for thepaymethod.The
ShoppingCartclass represents the context in which the payment strategy is used. It has apaymentStrategyfield that can be set dynamically using thesetPaymentStrategymethod. Thepaymethod of theShoppingCartclass delegates the payment processing to the chosen strategy.In the
Mainclass, we demonstrate the usage of the strategy pattern. We create aShoppingCartobject and set the payment strategy to a credit card. We then call the pay method, which internally invokes the payment strategy'spaymethod to process the payment. Similarly, we switch the payment strategy to PayPal and make another payment using the samepaymethod.By using the Strategy pattern, the shopping cart application becomes flexible and extensible. New payment strategies can be added without modifying the existing code, and it allows for easy switching between different payment methods at runtime.