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.

the Play Button. In a typical video player application, the player will start over again. In<br />

that application, the startPlay( ) method is as dumb as a boxof rocks. However,<br />

because polymorphism allows multiple forms of the same method, it allows us to provide<br />

the application with multiple forms that know what to do in different contexts. So<br />

in the Play state, the method knows it’s already playing, and so it does nothing. A user<br />

can pound on the Play Button all he wants, and the video just keeps on playing. He can<br />

press the Stop Button, and the video will stop, just like it’s supposed to do.<br />

To better appreciate polymorphism, you will see that as more states are added to the<br />

application, there’s more to keep track of. With more methods, we absolutely, positively<br />

do not want to modify the wrong thing. Without polymorphism, we run the risk<br />

of having the same method do something we definitely do not want it to do. Thus, if<br />

you don’t want your application to start playing video all over again every time the<br />

startPlay( ) method is invoked, using the State design pattern, you can structure the<br />

application to only start at the beginning when the originating state is the Stop state.<br />

Likewise, you can structure the startPlay( ) to begin playing all over again from the<br />

Play state—you write the code, and so you control how the methods behave.<br />

When you read the next chapter on the Strategy design pattern, you may have a<br />

major case of déjà vu. The juxtaposition of these two chapters is no accident. Once<br />

you <strong>com</strong>plete the example applications in each chapter, you should definitely get a<br />

different feel for each, even though the structures look very similar. The State design<br />

pattern has its focus on the states and well-defined transitions. This is one reason we<br />

use statecharts—they help clarify and simplify the architectural work in focusing on<br />

the different states and how they transition from one to another. The transitions in<br />

the State design can be controlled by the states themselves or by the context class.<br />

Also, because the State design creates a class for each state (behavior environment),<br />

you tend to generate more classes with the State design than with the Strategy<br />

design. Determining which behavior to use is delegated to the State classes, while the<br />

Strategy pattern encapsulates a family of algorithms and allows them to vary independent<br />

of the client within the structure that uses them.<br />

Minimalist Abstract State Pattern<br />

Using the State design pattern, all the behaviors (methods) for a single state are<br />

placed into single objects (concrete states), and all transition behaviors for the application<br />

(state machine) are placed into a single interface. Each state object implements<br />

the interface in a fashion appropriate for the state. Because of this structure,<br />

no conditional statements are required to branch differentially depending on the current<br />

state. Rather than writing <strong>com</strong>plexconditional statements, the individual state<br />

objects define how the methods are to behave for that state.<br />

Minimalist Abstract State Pattern | 361

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

Saved successfully!

Ooh no, something went wrong!