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
No comments:
Post a Comment