10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 CHAPTER 10<br />

State Pattern10<br />

A State without the means of some change is without<br />

the means of its conservation.<br />

—Edmund Burke<br />

All modern revolutions have ended in a reinforcement<br />

of the power of the State.<br />

—Albert Camus<br />

<strong>Design</strong> Pattern to Create a State Machine<br />

The State design pattern focuses on the different states in an application, transitions<br />

between states, and the different behaviors within a state. Looking at a simple light<br />

switch application, we can see two states, On and Off. In the Off state, the light is<br />

not illuminated, and in the On state, the light illuminates. Further, the light switch<br />

transitions from the Off state to the On state using a method that changes the application’s<br />

state—flipping the switch. Likewise, it transitions from On to Off with a different<br />

transition and method. An interface holds the transitions, and each state<br />

implements the transitions as methods unique to the state. Each method is implemented<br />

differently depending on the context of its use. So, a method,<br />

illuminateLight( ), for example, would do one thing in the Off state and something<br />

entirely different in the On state, even though illuminateLight( ) method is part of<br />

both states.<br />

Key Features<br />

The following key features characterize the State design pattern:<br />

• States exist internally as part of an object.<br />

• Objects change in certain ways when states change. Objects may appear to<br />

change classes, but they’re changing behavior that is part of the class.<br />

• Each state’s behavior depends on the current state of other states in the object.<br />

357

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!