State Design Pattern In Java

This tutorial explains the state design pattern with a simple example program. State design pattern is very similar to the Strategy pattern, the only difference is that state design pattern maintains the state of the context where as strategy pattern passes the context and execute the specific strategy.

The state design pattern is very useful in the scenarios where sequence of actions are taken with the pre-defined order. A perfect example is the lifecycle methods executed in a framework. Each phase is invoked one after the another by looking at the current phase of the context. I have used the lifecycle example for explaining the state design pattern example.

  • State operation will be declared as interface. Actual implementations will have the specific states with reference to the next state.
  • State context is where state instantiated with the initial state. State contact will declare a constructor to initialize state.
  • When you invoke the operation, it will keep moving to the different states for each execution.

State.java

package javabeat.net.pattern;

public interface State {
	public void execute(StateContext context);
}

StateOne.java

package javabeat.net.pattern;

public class StateOne implements State{
	public void execute(StateContext context){
		System.out.println("State One Executed!!");
		context.setState(new StateTwo());
	}
}

StateTwo.java

package javabeat.net.pattern;

public class StateTwo implements State {
	public void execute(StateContext context){
		System.out.println("State Two Executed!!");
		context.setState(new StateThree());
	}
}

StateThree.java

package javabeat.net.pattern;

public class StateThree implements State{
	public void execute(StateContext context){
		System.out.println("State Three Executed!!");
	}
}

StateContext.java

package javabeat.net.pattern;

public class StateContext {
	State state = null;
	public StateContext(State state){
		this.setState(new StateOne());
	}
	public void setState(State state){
		this.state = state;
	}

	public void runLifeCycle(){
		state.execute(this);
	}
}

StatePatternExample.java

package javabeat.net.pattern;

/**
 * State design pattern example
 *
 * @author Krishna
 *
 */
public class StatePatternExample {
	public static void main(String[] args) {
		//Create state context
		StateContext context = new StateContext(new StateOne());

		//Invoke operation from state context
		context.runLifeCycle();
		context.runLifeCycle();
		context.runLifeCycle();
	}
}

Output…

State One Executed!!
State Two Executed!!
State Three Executed!!
  • We have implemented State interface to execute each state.
  • StateOne, StateTwo and StateThree are the three states we have implemented using the State interface.
  • StateContext is the actual context where the state would be invoked. You can consider this as your main application.

You can run the StatePatternExample class to test the State Design Pattern.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*