State Pattern Example in Java

State Pattern

 

This pattern is used to encapsulate varying behavior for the same object based on its internal state.
 

Sample Implementation Code :

 

public interface State {
   public void doAction(Context context);
}
 
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";
   }
}
 
public class Context {
   private State state;

   public Context(){
      state = null;
   }

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

   public State getState(){
      return state;
   }
}
 
public class StatePatternDemo {
   public static void main(String[] args) {
      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());
   }
}
 

Output

 

Player is in start state
Start State
Player is in stop state
Stop State

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags