Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

How to implement state-dependent behavior

Doing the State pattern in Java

  • Print
  • Feedback

Page 4 of 5

Three states: Acclerating, Slowing Down, Stopping



The action associated with slowing down might be to press lightly on the brake. The action associated with stopping might be to press firmly on the brake. When leaving the Accelerating state, the action in both cases would be to remove your foot from the accelerator. Only then would you take the action appropriate for slowing down or stopping.

Note: The exit action for the Slowing Down state could also be "remove foot from brake." That would allow a transition from slowing down to accelerating even though, for stopping, the foot would be put right back on the brake. In a real-time program, such inefficiency would not be acceptable. But in many applications, such a "take it away and put it right back" technique is a useful stratagem that produces a correct result with very simple code. Often, the performance penalty is insignificant compared with the clarity of the result.

Implemented methods
Finally, here is a class that defines one of the actual states:

  class State1 extends State {
    void stateExit(StateOwner owner) {
    }

void stateEnter(StateOwner owner) { } }


This class defines the stateExit() and stateEnter() methods so they are ready to be filled in with the behaviors appropriate for the state.

Using the State class

Here is a sample of code for testing the State class:

  public class StateOwner {
    //Create the object and initialize its state
    public StateOwner() {
    this.stateVar = State.state1;
    stateVar.stateEnter(this);
    }
    // Set the new state
    public void setState(State newState) {
    stateVar.stateExit(this);
    this.stateVar = newState;
    stateVar.stateEnter(this);
    }
  private State stateVar;
  }


In this code, the constructor for the StateOwner class sets up the initial state and calls the entry method for that state.

The setState() method calls the exit method for the current state, sets the new state, and then calls the new state's entry method.

Finally, the private variable state stores the current state for the object.

(End of precept)

Final notes

Here are a couple of thoughts on how to use and extend this state pattern.

Encapsulating the state transitions
Using the State class requires calling the stateEnter() and stateExit() methods when the state changes. An alternative implementation would put everything that is currently in the StateOwner class into a new class called (for example), StateType. That way, the StateType object can encapsulate the state-transition behavior. The StateOwner class can then create a StateType object, calling the setState() method to change states. The object then can take care of doing the state transitions whenever the state changes. That's what object-oriented programming is all about!

In some cases, it is appropriate to do the state changes when the new state is the same as the old state. In other cases, setting the current state to the same value should be a "no-op" -- that is, it should have no effect. For such cases, the setState() method can compare the new state against the current state and simply return if they match.

  • Print
  • Feedback

Resources