Strategy Design Pattern

Simon FosterSimon Foster
3 min read

Understanding the Strategy Design Pattern.

In the world of software development, design patterns are essential tools that help developers solve common problems in a standardized way. One such pattern is the Strategy Design Pattern. This pattern is particularly useful when you need to define a family of algorithms, encapsulate each one, and make them interchangeable. Let’s dive into what the Strategy Design Pattern is, why it’s useful, and how you can implement it in your projects.

What is the Strategy Design Pattern?

The Strategy Design Pattern falls under the category of behavioural design patterns. It enables an algorithm’s behaviour to be selected at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

Why Use the Strategy Design Pattern?

The primary benefits of using the Strategy Design Pattern include:

  1. Flexibility: It allows you to change the algorithm or strategy being used without altering the code that uses the algorithm.

  2. Reusability: By encapsulating algorithms separately, you can reuse them across different parts of your application.

  3. Maintainability: It promotes cleaner code by adhering to the Single Responsibility Principle, making your code easier to maintain and extend.

How to Implement the Strategy Design Pattern

Let’s break down the implementation of the Strategy Design Pattern with a simple example. Suppose we are developing a flight simulator that supports multiple flying methods (e.g., Flapping like a bird, Jet Engine powered, and Warp Speed).

Step 1: Define the Strategy Interface

First, we define an interface that all payment strategies will implement.

public interface IFlyingStrategy {
    void Fly();
}

Step 2: Implement Concrete Strategies

Next, we create concrete classes that implement the IPaymentStrategy interface.

public class Bird : IFlyingStrategy {
    public void Fly() { 
        Console.WriteLine("Flying like a bird"); 
    } 
}

public class Plane : IFlyingStrategy {
    public void Fly() { 
        Console.WriteLine("Flying using 21st century aviation fuel"); 
    } 
}

public class StarShip : IFlyingStrategy {
    public void Fly() { 
        Console.WriteLine("Flying using warp engines"); 
    } 
}

Step 3: Create the Context Class

The context class uses a IFlyingStrategy to perform the payment.

public class FlyContext {
    private IFlyingStrategy _flyingStrategy;

    public void SetPaymentStrategy(IFlyingStrategy flyingStrategy) {
        _flyingStrategy = flyingStrategy;
    }

    public void Fly() {
        _flyingStrategy.Fly();
    }
}

Step 4: Use the Strategy Pattern

Finally, we use the FlyContext to fly with different strategies.

class Program {
    static void Main(string[] args) {
        FlyContext context = new FlyContext();
        context.SetPaymentStrategy(new StarShip());
        context.Fly();
        context.SetPaymentStrategy(new Plane());
        context.Fly();
        context.SetPaymentStrategy(new Bird());
        context.Fly();
    }
}

Conclusion

The Strategy Design Pattern is a powerful tool that promotes flexibility, reusability, and maintainability in your code. By encapsulating algorithms and making them interchangeable, you can easily extend and modify your application without disrupting existing functionality. Whether you’re dealing with payment processing, flying machines, sorting algorithms, or any other scenario requiring dynamic behaviour, the Strategy Design Pattern can help you write cleaner and more efficient code.

0
Subscribe to my newsletter

Read articles from Simon Foster directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Simon Foster
Simon Foster

My real name is Simon Foster but my on-line persona is Funky Si a nickname I was given during my university days which has somehow stuck. I am a Developer working in the North of England. I have been working in IT departments since 2006 and have a wide range of experiences. More recently in the Software Development space but also SysAdmin, Server Infrastructure, Databases and DevOps.