Design pattern "Strategy"

Let's learn what is the "Strategy" design pattern
Sunday, August 18, 2024

Real world example

In an e-commerce application, customers might want to pay for their purchases using different payment methods. Some customers might want to pay using a credit card, others might prefer PayPal, and some might even want to use Bitcoin. Each of these payment methods has a different process for transferring money from the customer to the merchant.

The Strategy pattern allows you to encapsulate each of these payment processes into its own class (a β€œstrategy”) that implements a common interface. This interface might have a method like pay(amount), which each payment method class implements in its own way.

In plain words

Strategy pattern allows you to switch the algorithm or strategy based upon the situation.

Wikipedia definition

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables an algorithm’s behavior to be selected at runtime.

Programmatic example

// Strategy Interface
interface PaymentStrategy {
public void pay(int amount);
}
// Concrete Strategy 1
class CreditCardStrategy implements PaymentStrategy {
@Override
public void pay(int amount) {
System.out.println("Paid " + amount + " using Credit Card.");
}
}
// Concrete Strategy 2
class PaypalStrategy implements PaymentStrategy {
@Override
public void pay(int amount) {
System.out.println("Paid " + amount + " using PayPal.");
}
}
// Concrete Strategy 3
class BitcoinStrategy implements PaymentStrategy {
@Override
public void pay(int amount) {
System.out.println("Paid " + amount + " using Bitcoin.");
}
}
// Context
class ShoppingCart {
private PaymentStrategy paymentMethod;
public void setPaymentMethod(PaymentStrategy paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void checkout(int amount) {
paymentMethod.pay(amount);
}
}
// Client code
class Main {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
// Pay by credit card
cart.setPaymentMethod(new CreditCardStrategy());
cart.checkout(100);
// Pay by PayPal
cart.setPaymentMethod(new PaypalStrategy());
cart.checkout(200);
// Pay by Bitcoin
cart.setPaymentMethod(new BitcoinStrategy());
cart.checkout(300);
}
}

Diagram

Loading graph...

Some other examples

Here are some real-world other examples where the Strategy design pattern can be useful:

  1. Sorting Algorithms: In a data processing application, you might want to sort data in different ways based on the context. You can use the Strategy pattern to switch between QuickSort, MergeSort, BubbleSort, etc.
  2. Compression Algorithms: In a file storage system, you might want to compress files using different algorithms based on the file type or other factors. You can use the Strategy pattern to switch between ZIP, RAR, TAR, etc.
  3. Payment Methods: In an e-commerce application, you can use the Strategy pattern to switch between different payment methods like Credit Card, PayPal, Bitcoin, etc.
  4. Routing Algorithms: In a GPS application, you can use the Strategy pattern to switch between different routing algorithms like Shortest Path, Least Traffic, Scenic Route, etc.
  5. Image Filters: In a photo editing application, you can use the Strategy pattern to switch between different image filters like Sepia, Black and White, Brightness Increase, etc.
  6. AI Behaviors: In a video game, you can use the Strategy pattern to switch between different AI behaviors like Aggressive, Defensive, Neutral, etc.
  7. Validation Rules: In a form validation system, you can use the Strategy pattern to switch between different validation rules like Email Validation, Phone Number Validation, SSN Validation, etc.
  8. Tax Calculation: In a financial system, you can use the Strategy pattern to switch between different tax calculation strategies based on the country or state.
  9. Shipping Methods: In a logistics system, you can use the Strategy pattern to switch between different shipping methods like Ground Shipping, Air Freight, Sea Freight, etc.
  10. Database Access Strategies: In a database access layer, you can use the Strategy pattern to switch between different database access strategies like Direct SQL, Stored Procedure, Entity Framework, etc.

Recommended articles