Strategy Pattern in C#

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets you choose the algorithm to use at runtime without changing the code that uses it.

Real-Life Analogy: Navigation App Routes

Think about using a navigation app like Google Maps. You type in your destination — but then choose how you want to get there. You might pick the fastest route, shortest route, or a scenic route that avoids highways.

The app uses a different strategy depending on what you select, but the goal is the same: get you from point A to point B. You can change your strategy anytime, and the rest of the app doesn’t need to change. That’s exactly how the Strategy Pattern works.

Navigation app offering multiple route strategies
Like a navigation app switching between fastest or scenic routes, the Strategy Pattern allows objects to change behaviour by choosing from multiple interchangeable algorithms.

Benefits of Strategy Pattern

This pattern makes code more flexible and easier to extend. Instead of hard-coding behaviours, you define them as separate classes and inject the one you need.

You can add new algorithms without touching existing code, which follows the open/closed principle.

  • Eliminates complex conditionals and switch statements
  • Makes behaviours reusable across different contexts
  • Allows changing algorithms at runtime

When Should You Use It?

Use the Strategy Pattern when you have different ways of doing the same task, and you want to switch between them easily. It's great when behaviour can vary between use cases but should follow a shared interface.

It suits scenarios such as:

  • Sorting data using different algorithms
  • Payment processing with multiple providers
  • Gameplay or AI decisions that can vary by mode

When Not to Use It: Avoid it when there's only one algorithm or the logic is unlikely to change — the added abstraction would be overkill.

What to Implement

To use this pattern, you need:

An interface for the strategy, multiple concrete strategy classes that implement it, and a context class that delegates work to the current strategy.

  • Strategy Interface: Declares the operation
  • Concrete Strategies: Implement specific logic
  • Context: Uses the strategy and delegates to it

How It Works in C#


// Strategy Interface
public interface IRouteStrategy
{
    void BuildRoute(string from, string to);
}

// Concrete Strategies
public class FastestRoute : IRouteStrategy
{
    public void BuildRoute(string from, string to)
    {
        Console.WriteLine($"Calculating fastest route from {from} to {to}");
    }
}

public class ScenicRoute : IRouteStrategy
{
    public void BuildRoute(string from, string to)
    {
        Console.WriteLine($"Calculating scenic route from {from} to {to}");
    }
}

// Context
public class Navigator
{
    private IRouteStrategy _strategy;

    public Navigator(IRouteStrategy strategy) => _strategy = strategy;

    public void SetStrategy(IRouteStrategy strategy) => _strategy = strategy;

    public void Navigate(string from, string to)
    {
        _strategy.BuildRoute(from, to);
    }
}

Usage:


var navigator = new Navigator(new FastestRoute());
navigator.Navigate("Home", "Office");

navigator.SetStrategy(new ScenicRoute());
navigator.Navigate("Home", "Park");

Final Thoughts

The Strategy Pattern helps you choose the right behaviour on demand — just like selecting the best route in your map app.

It keeps your code clean, testable, and open to new ideas — without needing to rewrite everything when your needs change.

Strategy Pattern in C# | SimplyAdvanced.dev