Java Design Patterns Tutorial - Java Design Pattern - State Pattern








In State pattern a class behavior is changed based on its state.

State pattern is a behavior pattern.

When using State pattern, we create various state objects and a context object whose behavior varies as its state object changes.

Example

interface State {
  public void doAction(Context context);
}/*from   w  w  w  . j a v a  2s.c  o m*/

class StartState implements State {
  public void doAction(Context context) {
    System.out.println("In start state");
    context.setState(this);
  }

  public String toString() {
    return "Start State";
  }
}

class StopState implements State {

  public void doAction(Context context) {
    System.out.println("In stop state");
    context.setState(this);
  }

  public String toString() {
    return "Stop State";
  }
}

class PlayState implements State {
  public void doAction(Context context) {
    System.out.println("In play state");
    context.setState(this);  
  }
  public String toString() {
    return "Play State";
  }
}

class Context {
  private State state;

  public Context() {
    state = null;
  }

  public void setState(State state) {
    this.state = state;
  }

  public State getState() {
    return state;
  }
}

public class Main {
  public static void main(String[] args) {
    Context context = new Context();

    StartState startState = new StartState();
    startState.doAction(context);

    System.out.println(context.getState().toString());

    PlayState playState = new PlayState();
    playState.doAction(context);
    
    StopState stopState = new StopState();
    stopState.doAction(context);

    System.out.println(context.getState().toString());
  }
}

The code above generates the following result.