tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Design Patterns > Java Design Patterns > State : Switch Example

State : Switch Example 

State pattern comes under Behavioral design pattern. It lets an object to change its behavior when its internal state changes.

Behaviour & Advantages

  • Supports state specific behavior.
  • Defines an object oriented state machine.
  • The state context hold a reference to current state.
Participants
  • State Context
    Maintains a reference to current state which is initialized with an initial state. It lets the clients change its state by exposing some API.
  • State
    Abstract interface for defining state specific behavior.
  • Concrete State
    An implementation of State specific behavior.

This examples shows a toggle switch. Here StateContext class acts as State Context and IState interface acts as State. SwitchOff and SwitchOn classes implement IState which acts as Concrete States. StateContext is instantiated with an initial state SwitchOff.

State interface is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state2/IState.java 
   
package com.bethecoder.tutorials.dp.state2;

public interface IState {

  /**
   * Toggle current state. 
   */
  public void toggle(StateContext stateContext);
  
}
   

State context class is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state2/StateContext.java 
   
package com.bethecoder.tutorials.dp.state2;

public class StateContext {
  /**
   * Current state.
   */
  private IState currentState = null;
  
  public StateContext() {
    /**
     * Set initial state to switch off.
     */
    setCurrentState(new SwitchOff());
  }

  public void toggleSwitch() {
    currentState.toggle(this);
  }
  
  public IState getCurrentState() {
    return currentState;
  }

  public void setCurrentState(IState currentState) {
    this.currentState = currentState;
  }
  
}
   

SwitchOff Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state2/SwitchOff.java 
   
package com.bethecoder.tutorials.dp.state2;

public class SwitchOff implements IState {

  @Override
  public void toggle(StateContext stateContext) {
    //Some behavior specific to this state
    
    //Finally toggle the state
    stateContext.setCurrentState(new SwitchOn());    
  }

  public String toString() {
    return "Switch Off";
  }
}
   

SwitchOn Concrete State Implementation.

File Name  :  
com/bethecoder/tutorials/dp/state2/SwitchOn.java 
   
package com.bethecoder.tutorials.dp.state2;

public class SwitchOn implements IState {

  @Override
  public void toggle(StateContext stateContext) {
    //Some behavior specific to this state
    
    //Finally toggle the state
    stateContext.setCurrentState(new SwitchOff());
  }

  public String toString() {
    return "Switch On";
  }
}
   

State pattern usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/state2/Test.java 
   
package com.bethecoder.tutorials.dp.state2;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {
    StateContext stateContext = new StateContext();
    System.out.println(stateContext.getCurrentState());
    
    stateContext.toggleSwitch();
    System.out.println(stateContext.getCurrentState());

    stateContext.toggleSwitch();
    System.out.println(stateContext.getCurrentState());

    stateContext.toggleSwitch();
    System.out.println(stateContext.getCurrentState());

    stateContext.toggleSwitch();
    System.out.println(stateContext.getCurrentState());
  }

}
   

It gives the following output,
Switch Off
Switch On
Switch Off
Switch On
Switch Off


                    Print

   



Books Recommendation

 
 
bl  br