Saturday 1 February 2014

State Design pattern - Implementation [LED TV]




Click here to watch in Youtube : https://www.youtube.com/watch?v=RZ_RI602pMY

Click the below Image to Enlarge
State Design pattern - Implementation [LED TV]

State Design pattern - Implementation [LED TV]
State Design pattern - Implementation [LED TV] - Class Diagram














































StateOfLedTv.java

public interface StateOfLedTv
{

public void doTurnOnOrOff();

}


LEDTVOffState.java

public class LEDTVOffState implements StateOfLedTv
{

@Override
public void doTurnOnOrOff()
{
System.out.println("TV is turned OFf");
}

}

LEDTVOnState.java

public class LEDTVOnState implements StateOfLedTv
{

@Override
public void doTurnOnOrOff()
{
System.out.println("TV is turned ON");
}

}


LEDTVContext.java

public class LEDTVContext implements StateOfLedTv
{

private StateOfLedTv stateOfLedTv;

public StateOfLedTv getStateOfLedTv()
{
return stateOfLedTv;
}

public void setStateOfLedTv(StateOfLedTv stateOfLedTv)
{
this.stateOfLedTv = stateOfLedTv;

}

@Override
public void doTurnOnOrOff()
{
System.out.println("Current state : " + stateOfLedTv.getClass().getName());
stateOfLedTv.doTurnOnOrOff();

}

}

TVRemote.java

public class TVRemote
{

public static void main( String[] args )
{
LEDTVContext context = new LEDTVContext();
StateOfLedTv ledTvOnState = new LEDTVOnState();
StateOfLedTv ledTvOffState = new LEDTVOffState();

context.setStateOfLedTv(ledTvOnState);
context.doTurnOnOrOff();

context.setStateOfLedTv(ledTvOffState);
context.doTurnOnOrOff();
}

}

Output

Current state : LEDTVOnState
TV is turned ON
Current state : LEDTVOffState
TV is turned OFf

See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [TV Remote]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [ATM]
  • State Design pattern - Implementation [Vending Machine]
  • State Design pattern - Implementation [Project]
  • State Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment