You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Output
Payment using 'Credit Card': 100 euro
Payment successful.
Payment using 'Debit Card': 200 euro
Payment successful.
Payment using 'Debit Card': 900 euro
Payment unsuccessful.
Payment using 'Credit Card': 900 euro
Payment successful.
Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software.
The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable.
Strategy is often used to implement the various algorithms (sometimes called "strategies") for a particular operation (for example, searching a file, or sorting a collection).
Similarity with other patterns
Many patterns can be considered as a particular implementation of the strategy pattern.
The strategy pattern is a particularly straightforward example of dependency injection.
The strategy pattern is also known as the policy pattern. It is a particular implementation of the more general handle/body idiom.
The strategy pattern is analogous to the strategy pattern in architecture.