Design Patterns - State

design-patterns

Design Patterns - Behavioral - State.

In State pattern a class behavior changes based on its state. This type of 
design pattern comes under behavior pattern.  In State pattern, we create 
objects which represent various states and a context object whose behavior 
varies as its state object changes.

We are going to create a State interface defining an action and concrete state 
classes implementing the State interface. Context is a class which carries a 
State.

// Step 1: Create an interface.
public interface State {
   public void doAction(Context context);
}

// Step 2: Create concrete classes implementing the same interface.
public class StartState implements State {
   public void doAction(Context context) {
      System.out.println("Player is in start state");
      context.setState(this);    
   }

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

public class StopState implements State {
   public void doAction(Context context) {
      System.out.println("Player is in stop state");
      context.setState(this);    
   }

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

// Step 3: Create Context Class.
public class Context {
   private State state;

   public Context(){
      state = null;
   }

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

   public State getState(){
      return state;
   }
}

// Step 4: Use the Context to see change in behaviour when State changes
Context context = new Context();

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

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

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

System.out.println(context.getState().toString());
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License