Strategy design pattern for beginners.

Problem statement –

This is one of the most straightforward design pattern from GoF. I am sure we all have played video games at some point of time in our life. Maybe on video game console, or in mobile or in computer screen, or if you are rich, then perhaps in SP3 or Xbox. And yes, many of us still like to play them.

Remember, in the starting of any game. It asks you many questions. To configure the game according to your choice. Standard configuration in all games is “Difficulty level? Easy, medium, hard”. If we are playing it the first time, then we use to select easy. Otherwise, according to our comfort level with that particular game.

We can see a noticeable difference between easy and hard levels of difficulty. Program (game) works differently according to your input, on difficulty levels.

So, whenever you have such a requirement where you want to execute different algorithms depending upon the strategy set by the user. Or you have more than one way to solve a problem. And want user to pick an algorithm of his/her choice at run time. We can achieve that through strategy design pattern.

About strategy design pattern –

It is also known as policy pattern. We actually apply strategy pattern at all the time in our day to day life. While choosing a path to reach some destination. We notice peak traffic hours and strategize the path. If you are Indian and Hindu, then you will not eat non-vegetarian food on Tuesdays :) and during "Navratris". Those all are nothing but simple-simple strategies we decide in our day to day life.

UML Structure -

strategy DP
UML structure of strategy design pattern.

Example - 

strategy Dp example
Example of  strategy design pattern (Click on picture to see zoom view)



#include <iostream>
using namespace std;

class Mario
{
  public:
    virtual void play() = 0;
};

class EasyMario : public Mario
{
  public:
    void play()
    {
      cout<< "Easy Mario play is here "<<endl;
    }
};

class MediumMario : public Mario
{
  public:
    void play()
    {
      cout<< "Medium Mario play is here "<<endl;
    }
};

class HardMario : public Mario
{
  public:
    void play()
    {
        cout<< "Hard Mario play is here "<<endl;
    }
};

class PlayMario
{
  public:
    PlayMario(Mario* mario) : m_mario(mario) {}
    PlayMario(): m_mario(NULL) {}
    void setLevel(Mario* mario)
    {
      m_mario = mario;
    }
    void play()
    {
      m_mario->play();
    }
  private:
    Mario* m_mario;
};

/*
#######################################################################################
#########  __  __       _        ######################################################
######### |  \/  | __ _(_)_ __   ######################################################
######### | |\/| |/ _` | | '_ \  ######################################################
######### | |  | | (_| | | | | | ######################################################
######### |_|  |_|\__,_|_|_| |_| ######################################################
#######################################################################################
*/
int main(int argc, char** argv)
{
  int difficultyLaves = 0;
  cout<<"Hi welcome to game Maroi, please select Difficulty level "<<endl;
  cout<<"1 for easy "<<endl;
  cout<<"2 for medium "<<endl;
  cout<<"3 for hard "<<endl;
  cin>>difficultyLaves;
  Mario* mario;
  PlayMario playMario;
  switch(difficultyLaves)
  {
    case 1:
        mario = new EasyMario;
        playMario.setLevel(mario);
        playMario.play();
        break;
    case 2:
        mario = new MediumMario;
        playMario.setLevel(mario);
        playMario.play();
        break;
    case 3:
        mario = new HardMario;
        playMario.setLevel(mario);
        playMario.play();
        break;
    default:
        cout<<"you have entered wrong choice "<< endl;
  }
  return 0;
}

///////////////////////////////////////////////////////////////////-------------OutPut
/*
$ ./a.exe
Hi welcome to game Maroi, please select Difficulty level
1 for easy
2 for medium
3 for hard
3
Hard Mario play is here

*/

Thanks for reading it. To read more on design patterns and basic design principles please see my web page.

Comments

  1. One of the best and easiest explanation I have found over the web.

    Cheers!

    ReplyDelete

Post a Comment

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.