You can see this and other great articles on design patterns here.

The Strategy Design Pattern allows you to change the behavior of an application when given a context. The context is the outer shell the client code calls, and the behavior are defined by the strategy classes. The strategy pattern allows you to decouple the outer context from the internal behaviors.

Let’s look at the UML of the strategy pattern first, and then we will look at an example to see how it works. Below is the UML of the strategy pattern:

  • The IStrategy interface defines the behaviors supported.                 
    • It has the Algorithm method that specifies the behavior.
  • The ConcreteStrategy class are classes that implements the behaviors. You will have multiple ConcreteStrategy classes where each class implements a behavior.
    • It has the Algorithm method that implements the behavior.
  •  The Context class defines the situation to use the behaviors.
    • It has the strategy variable that points to the behavior of the Context. 

Let’s see an example. We would like to create a chess game with different levels of difficulty. The computer will respond based on the level of difficulty chosen by the user. The UML for the chess game will be:

  • The IComputerPlayer interface defines the behaviors supported.
    • It has the MakeMove method, which makes a move for the computer player.
  • The EasyComputerPlayer, MediumComputerPlayer, AdvancedComputerPlayer are the different difficulty levels.
    • It has the MakeMove method that implements the move the computer makes.
  •  The ChessGame class defines the situation to use the computer players.
    • It has the computerPlayer variable that holds a IComputerPlayer, which can be any level of difficulties.

The benefit of the strategy pattern is it allows you to choose the behavior of the application at runtime. You only need to define the behaviors as strategy classes, and the client code can simply choose any of the classes to exhibit the behavior.

A comparison between the UML of the strategy pattern and the state pattern shows striking similarities. Both pattern defines the behaviors as the concrete classes, and both pattern defines the context in which the behavior runs under. But there are clear differences. The strategy pattern lets the client code choose the behavior it needs, while the state pattern uses the behavior to switch to other behaviors.  

Below are the implementation code and the output using our chess game example. Notice that you can switch to different level of difficulties, or behavior, at anytime during the course of the chess game:

class Program
{
    static void Main(string[] args)
    {
        ChessGame game = new ChessGame();

        game.ComputerPlayer = new EasyComputerPlayer();
        game.Move();   //move using Easy difficulty

        game.ComputerPlayer = new MediumComputerPlayer();
        game.Move();   //move using Medium difficulty

        game.ComputerPlayer = new AdvancedComputerPlayer();
        game.Move();   //move using Advanced difficulty
    }
}


public interface IComputerPlayer
{
    void MakeMove();
}

public class EasyComputerPlayer : IComputerPlayer
{
    void IComputerPlayer.MakeMove()
    {
        Console.WriteLine("Computer made an Easy move.");
    }
}

public class MediumComputerPlayer : IComputerPlayer
{
    void IComputerPlayer.MakeMove()
    {
        Console.WriteLine("Computer made an Medium move.");
    }
}

public class AdvancedComputerPlayer : IComputerPlayer
{
    void IComputerPlayer.MakeMove()
    {
        Console.WriteLine("Computer made an Advanced move.");
    }

}

public class ChessGame
{
    private IComputerPlayer computerPlayer;

    public IComputerPlayer ComputerPlayer
    {
        get { return computerPlayer; }
        set { computerPlayer = value; }
    }

    public void Move()
    {
        computerPlayer.MakeMove();   //exhibit the behavior
    }
}

Liked this article? You can see this and other great articles on design patterns here.

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架