Java Design Pattern: State

State Design Pattern is mainly for changing state at run-time.

State pattern story

People can live with different financial status. They can be rich or they can be poor. The two states - rich and poor - can be converted to each other from time to time. The idea behind the example: people normally work harder when they are poor and play more when they are rich. What they do depends on the state in which they are living. The state can be changed based on their actions, otherwise, the society is not fair.

State pattern class diagram

Here is the class diagram. You can compare this with strategy pattern to get better understanding of the difference.

state-pattern-class-diagram

State pattern Java code

The Java example below shows how State pattern works.

State classes.

package com.programcreek.designpatterns.state;
 
interface State {
	public void saySomething(StateContext sc);
}
 
class Rich implements State{
	@Override
	public void saySomething(StateContext sc) {
		System.out.println("I'm rick currently, and play a lot.");
		sc.changeState(new Poor());
	}
}
 
class Poor implements State{
	@Override
	public void saySomething(StateContext sc) {
		System.out.println("I'm poor currently, and spend much time working.");
		sc.changeState(new Rich());
	}
}

StateContext class

package com.programcreek.designpatterns.state;
 
public class StateContext {
	private State currentState;
 
	public StateContext(){
		currentState = new Poor();
	}
 
	public void changeState(State newState){
		this.currentState = newState;
	}
 
	public void saySomething(){
		this.currentState.saySomething(this);
	}
}

Main class for testing

import com.programcreek.designpatterns.*;
 
public class Main {
	public static void main(String args[]){
		StateContext sc = new StateContext();
		sc.saySomething();
		sc.saySomething();
		sc.saySomething();
		sc.saySomething();
	}
}

Result:

I'm poor currently, and spend much time working. 
I'm rick currently, and play a lot.
I'm poor currently, and spend much time working. 
I'm rick currently, and play a lot.
Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. omoran on 2013-11-23

    I see ugly code formatting, please fix it.

  2. ryanlr on 2013-11-23

    Fixed. Thanks!

Leave a comment

*