State Design Pattern
It is one the behavioural design pattern
The state design pattern is a design pattern that allows an object to completely change its behavior depending upon its current internal state.
The state design pattern change the object's behavior at run-time without changing the interface used to access the object or losing the current state. The class change is hidden to outside world with the use of wrapper object.
Its also known as state machine.
Example:
In game design; we have many screens/state main-menu, loading screen, game-play, in game menu screen, result screen etc. To maintain different screen state; we use state design pattern to traverse different screen.
Example code
#include<iostream>
using namespace std;
class GameState {
public:
virtual void handleKey()=0;
virtual void handleMouse()=0;
};
class Game {
GameState *currentScreen;
public:
Game(GameState *state)
{
currentScreen = state;
}
void handleKeyEvent()
{
currentScreen->handleKey();
}
void handleMouseEvent()
{
currentScreen->handleMouse();
}
void setState(GameState* state)
{
cout<<endl<<"current screen is changeed"<<endl<<endl;
currentScreen = state;
}
};
class GamePlay : public GameState
{
public:
void handleKey()
{
cout<<"key event handle in GamePlay state"<<endl;
}
void handleMouse()
{
cout<<"Mouse event handle in Gameplay state"<<endl;
}
};
class InGameMenu : public GameState
{
public:
void handleKey()
{
cout<<"key event handle in InGameMenu state"<<endl;
}
void handleMouse()
{
cout<<"mouse event handle in InGameMenu state"<<endl;
}
};
int main()
{
GameState *igm = new InGameMenu();
GameState *play = new GamePlay();
Game game(igm);
game.handleKeyEvent();
game.handleMouseEvent();
game.setState(play);
game.handleKeyEvent();
game.handleMouseEvent();
return 0;
}
Output:
key event handle in InGameMenu state
mouse event handle in InGameMenu state
current screen is changeed
key event handle in GamePlay state
Mouse event handle in Gameplay state
The state design pattern is a design pattern that allows an object to completely change its behavior depending upon its current internal state.
The state design pattern change the object's behavior at run-time without changing the interface used to access the object or losing the current state. The class change is hidden to outside world with the use of wrapper object.
Its also known as state machine.
Example:
In game design; we have many screens/state main-menu, loading screen, game-play, in game menu screen, result screen etc. To maintain different screen state; we use state design pattern to traverse different screen.
#include<iostream>
using namespace std;
class GameState {
public:
virtual void handleKey()=0;
virtual void handleMouse()=0;
};
class Game {
GameState *currentScreen;
public:
Game(GameState *state)
{
currentScreen = state;
}
void handleKeyEvent()
{
currentScreen->handleKey();
}
void handleMouseEvent()
{
currentScreen->handleMouse();
}
void setState(GameState* state)
{
cout<<endl<<"current screen is changeed"<<endl<<endl;
currentScreen = state;
}
};
class GamePlay : public GameState
{
public:
void handleKey()
{
cout<<"key event handle in GamePlay state"<<endl;
}
void handleMouse()
{
cout<<"Mouse event handle in Gameplay state"<<endl;
}
};
class InGameMenu : public GameState
{
public:
void handleKey()
{
cout<<"key event handle in InGameMenu state"<<endl;
}
void handleMouse()
{
cout<<"mouse event handle in InGameMenu state"<<endl;
}
};
int main()
{
GameState *igm = new InGameMenu();
GameState *play = new GamePlay();
Game game(igm);
game.handleKeyEvent();
game.handleMouseEvent();
game.setState(play);
game.handleKeyEvent();
game.handleMouseEvent();
return 0;
}
Output:
key event handle in InGameMenu state
mouse event handle in InGameMenu state
current screen is changeed
key event handle in GamePlay state
Mouse event handle in Gameplay state

Comments
Post a Comment