Chapter 12. State Pattern


In This Chapter

Simple State Example 181

Encapsulating the States 184

Using Abstract Classes 188

Transitions 193

Summary 198

The State pattern is a valuable pattern in ActionScript development. It allows an object to change its behavior when its internal state changes. Take a toggle button as an example. The toggle button must maintain two separate states: one for selected and one for unselected. The two states share the same structure, but they have very different functionality. In addition to the visual display being different, they most likely handle a click event differently, too. This is where the State pattern becomes valuable. The State pattern defines a standard methodology for handling encapsulated states.

There are many ways to implement the State pattern, but they all come to the same result: The object's class appears to change. Obviously, we aren't talking about changing the object's type at runtime, but we are talking about changing nearly every operation in a class based on its internal state.

The best way to demonstrate the State pattern is with an example. Therefore, we're going to build a basketball shooter. Our example will build a shooter object with three different states: lay up, free throw, and three-pointer. Each of these states has characteristics such as accuracy and point value. We'll use this shooter object in the context of a basketball game. When we tell the shooter to shoot the ball, we can calculate whether the shot was made and for how many points based on the internal state of the shooter object.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net