How to implement state-dependent behavior

Doing the State pattern in Java

1 2 Page 2
Page 2 of 2

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.

Enumerating the possible states

It may be desirable occasionally to enumerate all of the possible states. For example, you might want to write a loop, where the loop index is one of the possible states. It's not common that you want to do such a thing, but it's possible. To do this, the various State objects need links to one another, and the State class needs to define a method that returns an Enumeration object, which lets you iterate over the linked list of defined states. The mechanisms for doing that are defined in "Creating enumerated constants in Java."


The State pattern is a good addition to your bag of programming tricks, because it is so useful. There are many ways to implement the actions associated with state transitions. It is possible to define actions as part of transition-objects, or define them as part of state-objects. You can have behaviors that occur when exiting a state, when entering a state, or both. The emerging Unified Modeling Language standard (UML) allows for all three. In this article, you learned how to use the fundamental O-O concept of polymorphism to implement a version of the State pattern that associates actions with states and that allows both state-exit and state-enter actions. You also saw how to completely encapsulate the state transitions in a state object that can be owned by a higher-level "containing" class.

Eric Armstrong has been programming and writing professionally since before there were personal computers. His production experience includes AI programs, system libraries, real-time programs, and business applications in a variety of languages. He is currently writing a book on a soon-to-be-released Java IDE.

Learn more about this topic

This story, "How to implement state-dependent behavior" was originally published by JavaWorld.


Copyright © 1997 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2