Member-only story
The Strategy Pattern — Made Simple
For non-members you can access the article using this link.
The Strategy Pattern is a behavioral design pattern that enables selecting an algorithm’s behavior at runtime.
Instead of implementing a single algorithm directly, code receives run-time instructions specifying which of a family of algorithms to use.
Components
- Strategy Interface: this defines a common interface for all versions of an algorithm or behavior. The context uses this interface to call the algorithm defined by a ConcreteStrategy.
- Concrete Strategies: these are implementation classes for the strategy interface. Each implements a different variant of an algorithm or behavior.
- Context: the context maintains a reference to a strategy object and is configured with a ConcreteStrategy object. The context doesn’t know which strategy is being used; it just knows how to execute the strategy’s methods.
Scenario
Consider a payment processing system in an e-commerce application where the payment method (e.g., credit card, PayPal, cryptocurrency) can be changed based on user preference or other factors.
public interface PaymentStrategy {
void pay(int amount);
}
public class…