ActionScript 3.0 Design Patterns

(Chris Devlin) #1
357

Chapter 10
CHAPTER 10


State Pattern 10


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 appli-


cation’s state—flipping the switch. Likewise, it transitions from On to Off with a dif-


ferent transition and method. An interface holds the transitions, and each state


implements the transitions as methods unique to the state. Each method is imple-


mented 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 thoughilluminateLight( )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 of other states in the object.

Free download pdf