State
Design Pattern
State design pattern is used when an Object
change its behaviour based on its internal state.
State design pattern provides a mechanism to
change behavior of an object based on the object’s state.
Suppose you press the remote on/off button
following cases are possible:
- If TV is off it will turn ON
- If TV is ON it will turn OFF
public class TVBasic
{
private String currentState="OFF";
public String getCurrentState() {
return currentState;
}
public void
setCurrentState(String currentState) {
this.currentState = currentState;
}
public void
pressPowerButton(){
if(currentState.equalsIgnoreCase("ON")){
System.out.println("TV turned OFF");
currentState="OFF";
}else if(currentState.equalsIgnoreCase("OFF")){
System.out.println("TV
turned ON");
currentState="ON";
}
}
public static void main(String args[]){
TVBasic tvBasic = new TVBasic();
tvBasic.pressPowerButton();
tvBasic.pressPowerButton();
}
}
Meaning that when same function is executed
TV will behave differently based on the current state of TV.
Let this function be pressing the power
button on TV object
public static void
main(String[] args) {
TV
tv = new TV();
tv.pressPowerButton();
TV will behave differently based on the
present state of TV
The different states may be:
- TV OFF à TvOffState.Java
- TV ON à TvOnState.Java
ie in these two states when the power
button is pressed will behave in differently
We create a TV class having pressPowerButton() function.
We create two state objects also having
pressPowerButton() function so that they can react differently.
This TV class will delegate the call to
its current state Object.
public interface State {
public void
pressPowerButton();
}
public class TvOnState implements State {
TV
tv;
public TvOnState(TV tv) {
this.tv = tv;
}
@Override
/*
* In TvOnState if you press power button the
TV will turned OFF
* and its current state will be TvOffState
*
*/
public void pressPowerButton() {
System.out.println("TV
is turned OFF");
tv.setCurrentState(tv.getTvOffState());
}
}
public class TvOffState implements State{
TV
tv;
public TvOffState(TV tv) {
this.tv = tv;
}
@Override
/*
* In TvOffState if you press power button the
TV will turned ON
* and its current state will be set to TvOnState
*/
public void pressPowerButton() {
System.out.println("TV is turned ON");
tv.setCurrentState(tv.getTvOnState());
}
}
public class TV {
State
tvOnState;
State
tvOffState;
State
currentState ;
public TV() {
tvOffState = new TvOffState(this);
tvOnState = new TvOnState(this);
//assuming the TV is off
currentState = tvOffState;
}
/*
* When somebody presses the power button the
TV simply
* deligates the request to its current
state
*/
public void pressPowerButton() {
currentState.pressPowerButton();
}
public State getTvOnState() {
return tvOnState;
}
public State getTvOffState() {
return tvOffState;
}
public void setCurrentState(State state) {
this.currentState = state;
}
}
public class TestTV {
public static void
main(String[] args) {
TV
tv = new TV();
tv.pressPowerButton();
tv.pressPowerButton();
}
}
Suppose you are on ground floor and you
press the lift button following cases are possible:
- Lift will open immediately
- The LED will show down arrow and then lift will open after some time
- The LED will not down arrow and shows some number and after some time LED will show down arrow and then lift will open
Meaning that when same function is executed
lift will behave differently based on the present state of lift
The different states may be:
- Lift Idle
- Lift moving up
- Lift moving down
- Lift waiting to move up
- Lift waiting to move down
ie, Lift will open immediately if lift is
idle and standing on same floor and
The LED will show down arrow and then lift
will open after some time if lift is idle and standing on different floor
So the idea is to make the different state
Objects and have a same set methods in it but these methods will behave
differently.

Also there will be one central class which will create the objects of all the states and
invoke the method of the state. Also there will be one initial/current state
of central
class . Also state object have a control over changing the state of current
state of central class.
Every class and Interface except main class will have same method pressPowerBotton()
ReplyDelete