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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

One application where the state pattern’s popular is device simulation. Devices that<br />

change an object’s state are subject to change as the states change. The volume knob<br />

on a radio changes the sound’s volume state. More <strong>com</strong>plexsimulated devices<br />

include a music sound mixer board where simulated sliders change different states to<br />

affect the overall object (sound mixer) and the resulting sound. A Flash video player<br />

has several states to manage: play, record, append, pause and stop. Each state in the<br />

video player behaves according to the state of the other states as well as its own state.<br />

The State Model<br />

To understand and appreciate the value of the State design pattern, we need to<br />

understand something about State Machines. A State Machine is the general model of<br />

states you would be using in an application with the State design pattern. So if a<br />

video player application is designed around key states, the application would be the<br />

state machine. We can also refer to state engines that run the state machines. (Think<br />

of your automobile’s blueprints as a state machine, and the actual car as the state<br />

engine.) The programming we use to move from one state to another is the state<br />

engine. The state engine data structure defines mechanisms for handling messages<br />

and managing contexts.<br />

Rather than beginning with the usual diagrams associated with design patterns,<br />

we’re going to start with a statechart. At its most basic level, a statechart is an illustration<br />

of an application’s states and transitions, and as such is a model for the state<br />

machine and engine. Taking a simple video player application, we can see the Play<br />

and Stop states. When the application first runs, the application enters the Stop state<br />

and can only transition to the Play state. Figure 10-1 shows a statechart depicting<br />

this condition.<br />

358 | Chapter 10: State Pattern<br />

The illustration is <strong>com</strong>puter-drawn. The idea when sketching statecharts<br />

is to start with a rough idea, and then refine the idea with<br />

sketches. You’ll find that the process goes quicker using hand sketches<br />

with a pencil and scratch paper instead of a drawing program.<br />

Figure 10-1. Simple statechart showing states<br />

start<br />

Stop Play<br />

stop<br />

The line going from a black dot to the Stop state shows the Application-Not-Running<br />

state, but we’ll assume that the starting point is with the application running in<br />

the Stop state. This could be illustrated in a hierarchical state with Application

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

Saved successfully!

Ooh no, something went wrong!