Monday, December 18, 2006

The State Pattern

A class that switches between a set of states, and behaves differently based on the current state it is in, is a good candidate for the state pattern. The state pattern works by encapsulating the individual states along with the corresponding state changing logic into independent classes, thus removing/minimizing conditional logic. In Design Patterns, the authors define the State pattern as:
Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
The utility of the state pattern will not be obvious with simple examples that have a very few states and a little conditional logic to switch between states. Such simple state change can be implemented by using state variables and a little conditional logic in each method as shown below.
public class SimpleState {

private final String ON_STATE = "on";

private final String OFF_STATE = "off";

// The current state, default is off.
private String currentState = OFF_STATE;

public String redButton() {
if (currentState.equals(ON_STATE)) {
currentState = OFF_STATE;
return "State changed to off";
} else {
return "State was not changed";
}
}

public String greenButton() {
if (currentState.equals(OFF_STATE)) {
currentState = ON_STATE;
return "State changed to on";
} else {
return "State was not changed";
}
}

public static void main(String[] args) {
SimpleState simpleState = new SimpleState();
System.out.println(simpleState.redButton());
System.out.println(simpleState.greenButton());
System.out.println(simpleState.greenButton());
System.out.println(simpleState.redButton());
System.out.println(simpleState.redButton());
System.out.println(simpleState.greenButton());
}
}
SimpleState.java
This example has only two states and a couple of functions that affect the state changes, and consequently the behaviour of the SimpleState class. Even in this trivial example, you can see that adding a new state requires you to change the two methods. Moreover, in both the methods, the conditional logic will also tend to increase. The state pattern helps in this case by
  • Remove/minimize state-changing conditional logic
  • Provide a high-level view of the state changing logic
Given the advantages, the state pattern can also complicate your design when used in trivial cases when there are a very few states and you are sure that you wont be adding new states (atleast in the near future). It is not advisable to use State pattern in cases when the state changing logic is trivial.
State Pattern and Strategy Pattern
Although the state pattern looks very much like the strategy pattern, they differ in a few important aspects
  • The State Design Pattern can be seen as a self-modifying Strategy Design Pattern.
  • A change in state of a class may affect what it does, while a change is strategy of a class only changes how it does the same job.
  • In the state pattern, the state of the context object changes over time based on the a few well-defined conditions, while the strategy of a context object is set once, and is not expected to be changed.

7 comments:

  1. We’ve been looking for this information for a very long time.

    ReplyDelete
  2. Interesting post. I Have Been wondering about this issue, so thanks for posting.

    ReplyDelete
  3. I do trust all of the concepts you have presented to your post.

    ReplyDelete
  4. I like the valuable information you provide in your articles.

    ReplyDelete
  5. This is really interesting, You’re a very skilled blogger.

    ReplyDelete
  6. Thanks a lot for sharing this with all folks you really realize what you’re talking approximately! Bookmarked.

    ReplyDelete

Popular Posts