Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

How to implement state-dependent behavior

Doing the State pattern in Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
As someone who tends to think of objects as "data structures on steroids," it came as quite a shock when Netscape's Steve Abell pointed out that an object need not contain any values at all -- it can exist merely to provide behaviors, in the form of methods. This article shows how this concept, along with polymorphism (defined below), can be used to implement the archetypical State pattern in Java -- a pattern that finds many uses in a variety of different contexts.

Separating behaviors into disparate objects makes sense when the separation takes advantage of polymorphism. Polymorphism allows two objects to be treated identically, using the same methods, even though the objects implement these methods in quite different ways. It is this concept of "same appearance, different behavior" that gets the 0 word, polymorphism. (You can tell how important this concept is by the number of syllables the word has!)

Polymorphism works because the classes that implement the same method differently both derive, at some point, from a common superclass. A general program is written that operates on objects of the superclass type. The program is oblivious to the fact that what it thinks of as a superclass object is, in reality, an object that is a member of one of the subclass types. When the program invokes a method defined in the superclass, the method that gets called is actually the subclass method that overrides the superclass version.

Polymorphism allows very general programs to be written for a superclass, letting the subclasses take care of the details. An example of this is a drawing program that can write a loop that processes a list of graphic objects including lines, squares, and circles. It can work from the bottom most graphic object to the top, invoking the draw() method on each object in turn. The drawing program manages the sequence of drawing operations, which depends on which objects are "on top." Each object, in turn, is responsible for drawing itself. The program works on objects of type Graphic, knowing that every Graphic object always implements the draw() method. The subclasses Line, Square, and Circle, all derive from the superclass Graphic, and each overrides the draw() method in its own fashion. The program can then be extended by adding an Elipse or Rectangle class. As long as the new classes derive from Graphic, and as long as they implement the draw() method appropriately, the basic drawing program continues to work, unchanged.

(See Philip Bishop's JavaWorld article, "Polymorphism and Java," for more information on this subject.)

The State pattern, described in the book Design Patterns, by Gamma, Helm, Johnson, and Vlissides, Addison-Wesley, 1995 (see Resources for further book information), is an example of a tremendously useful software pattern that takes advantage of polymorphism. (The State pattern is just one of 23 useful patterns contained in that book.) The State pattern uses polymorphism to define different behaviors for different states of an object. It is a valuable pattern to master, because it can be used in practically any sizable application.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources