Click here to Skip to main content
Click here to Skip to main content
Go to top

Strategy Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Strategy Design Pattern clearly explained with example

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 8:32 
GeneralThank you for sharing PinmemberSouthmountain29-Apr-11 10:56 
GeneralMy vote is 5 PinmemberShahriar Iqbal Chowdhury29-Apr-11 5:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid