Click here to Skip to main content
13,291,510 members (61,985 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 25 Apr 2011

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.


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote is 5 Pin
Shahriar Iqbal Chowdhury29-Apr-11 6:01
memberShahriar Iqbal Chowdhury29-Apr-11 6:01 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid