Chapter 10. State Design Pattern

A State without the means of some change is without the means of its conservation.

Edmund Burke

All modern revolutions have ended in a reinforcement of the power of the State.

Albert Camus

Design Pattern to Create a State Machine

The State design pattern focuses on the different states in an application, transitions between states, and the different behaviors within a state. Looking at a simple light switch application, we can see two states, On and Off. In the Off state, the light is not illuminated, and in the On state, the light illuminates. Further, the light switch transitions from the Off state to the On state using a method that changes the application’s state—flipping the switch. Likewise, it transitions from On to Off with a different transition and method. An interface holds the transitions, and each state implements the transitions as methods unique to the state. Each method is implemented differently depending on the context of its use. So, a method, illuminateLight(), for example, would do one thing in the Off state and something entirely different in the On state, even though illuminateLight() method is part of both states.

Key Features

The following key features characterize the State design pattern:

  • States exist internally as part of an object.

  • Objects change in certain ways when states change. Objects may appear to change classes, but they’re changing behavior that is part of the class.

  • Each state’s behavior depends on the current state ...

Get ActionScript 3.0 Design Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.