DEV Community

ATHISANKARAKAILASH K
ATHISANKARAKAILASH K

Posted on

Strategy Design Pattern: A Beginner's Guide

Strategy Design Pattern in Software Engineering

Introduction

When building software, we often face situations where multiple algorithms or behaviors can be used to perform the same task. Instead of writing large if-else or switch statements, the Strategy Design Pattern allows us to encapsulate each algorithm into a separate class and choose the required one at runtime.

The Strategy Pattern is a Behavioral Design Pattern that promotes flexibility, maintainability, and clean code.


Real-World Example

Imagine you're using Google Maps.

When navigating to a destination, you can choose different travel modes:

  • 🚗 Car
  • 🚶 Walking
  • 🚲 Bicycle
  • 🚌 Public Transport

The destination remains the same, but the route calculation changes based on the selected travel mode.

Each travel mode represents a different strategy.


Problem Without Strategy Pattern

Suppose we create a navigation system like this:

if(mode.equals("Car"))
    calculateCarRoute();
else if(mode.equals("Bike"))
    calculateBikeRoute();
else if(mode.equals("Walk"))
    calculateWalkingRoute();
Enter fullscreen mode Exit fullscreen mode

As more travel modes are added, the code becomes lengthy, difficult to maintain, and violates the Open/Closed Principle.


Solution Using Strategy Pattern

Instead of keeping all the logic in one class, we create a separate class for each routing algorithm.

Examples:

  • CarStrategy
  • BikeStrategy
  • WalkingStrategy

The application simply chooses the required strategy at runtime.

This makes the system flexible and easy to extend.


Structure

           Strategy
              â–²
      -----------------
      |       |       |
 CarStrategy BikeStrategy WalkStrategy

              â–²
          NavigationApp
Enter fullscreen mode Exit fullscreen mode

Advantages

  • Easy to add new algorithms.
  • Eliminates large if-else statements.
  • Improves code readability.
  • Promotes code reusability.
  • Follows the Open/Closed Principle.

Disadvantages

  • Increases the number of classes.
  • Selecting the correct strategy adds a small amount of extra code.

Java Example

interface PaymentStrategy{
    void pay(int amount);
}

class CreditCard implements PaymentStrategy{
    public void pay(int amount){
        System.out.println("Paid ₹" + amount + " using Credit Card");
    }
}

class UPI implements PaymentStrategy{
    public void pay(int amount){
        System.out.println("Paid ₹" + amount + " using UPI");
    }
}

public class Main{
    public static void main(String[] args){
        PaymentStrategy payment = new UPI();
        payment.pay(500);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Paid ₹500 using UPI
Enter fullscreen mode Exit fullscreen mode

Applications

  • Payment systems (Credit Card, UPI, PayPal)
  • Navigation applications
  • Sorting algorithms
  • Compression techniques
  • Authentication methods

Conclusion

The Strategy Design Pattern is an excellent choice whenever an application needs multiple ways of performing the same task. By separating each algorithm into its own class, the code becomes cleaner, easier to maintain, and simpler to extend without modifying existing functionality.

If your application frequently changes behavior based on user choice, the Strategy Pattern is one of the best design patterns to use.

Top comments (0)