The Strategy Pattern is a behavioral pattern.
The strategy design pattern gives you the ability to define a family of algorithms, put each of them in a separate class, and make their objects interchangeable.
class ShoppingCart {
public void payUsingPayPal(String username, String password) {
// Implementation
}
public void payUsingBancontact(String pinCode) {
// Implementation
}
// ...
}
interface PaymentStrategy {
void pay(double amount);
}
class PayPalPaymentStrategy implements PaymentStrategy {
@Override
public void pay(double amount) {
// Implementation
}
}
class ShoppingCart {
// PayPalPaymentStrategy
// BancontactPaymentStrategy
// ...
public void pay(PaymentStrategy strategy) {
strategy.pay(1234);
}
}
- When you need to have x types of functions that have the same primary goal but behave differently.
- From creating x types of different functions that have the same goal.