BlackWaspTM
Design Patterns
.NET 1.1+

Strategy Design Pattern

The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements.

What is the Strategy Pattern?

The strategy pattern is a Gang of Four design pattern. This is a behavioural pattern as it defines a manner for controlling communication between classes or entities. The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time. This allows the behaviour of a program to change dynamically according to configuration details or user preferences. It also increases flexibility by allowing new algorithms to be easily incorporated in the future.

As an example, the strategy pattern could be used within the scoring system of a fictitious game. In the game, players must run around a circuit that includes a series of checkpoints. At each checkpoint, the player must throw projectiles at a target. Points are scored for successful hits and removed for a slow time around the entire circuit. It may be that men, women and children use different scoring systems. In this case, a set of scoring algorithms could be developed and the decision of which to use be made at run-time, according to the sex and age of each competitor.

Implementing the Strategy Pattern

Strategy Design Pattern UML

The UML class diagram above describes an implementation of the strategy design pattern. The items in the diagram are described below:

  • Client. This class is the user of an interchangeable algorithm. The class includes a property to hold one of the strategy classes. This property will be set at run-time according to the algorithm that is required.
  • StrategyBase. This abstract class is the base class for all classes that provide algorithms. In the diagram the class includes a single method. However, there is no reason why a number of properties and methods may not be included. This class may be implemented as an interface if it provides no real functionality for its subclasses.
  • ConcreteStrategy A/B. The concrete strategy classes inherit from the StrategyBase class. Each provides a different algorithm that may be used by the client.

The following shows the basic code of the strategy design pattern implemented using C#. For brevity, the Client class' Strategy property is declared using C# 3.0 automatically implemented property syntax. For earlier versions of the .NET framework, it would be necessary to expand this syntax.

public class Client
{
    public StrategyBase Strategy { get; set; }

    public void CallAlgorithm()
    {
        Console.WriteLine(Strategy.Algorithm());
    }
}


public abstract class StrategyBase
{
    public abstract string Algorithm();
}


public class ConcreteStrategyA : StrategyBase
{
    public override string Algorithm()
    {
        return "Concrete Strategy A";
    }
}


public class ConcreteStrategyB : StrategyBase
{
    public override string Algorithm()
    {
        return "Concrete Strategy B";
    }
}
4 May 2009