
When you create a Flash Media Server 2 application, you typically place emphasis on optimizing the quality of the communications. That is certainly as it should be. Likewise, quality object-oriented programming (OOP) is another priority. One standard in OOP is design patterns—abstract concepts for solving recurring problems using designs that optimize OOP when you apply them appropriately. The seminal work in design patterns is Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm Ralph Johnson, and John Vlissides, affectionately known as "The Gang of Four" or simply GoF.
One design pattern in particular, the state design pattern (or SDP), focuses on the different states in an application, transitions between states, and the different behaviors within a state. A simple Flash video player application, for example, has two states: Stop and Play. In the Stop state, the video is not playing; in the Play state, a video is playing. Furthermore, the player transitions from the Stop state to the Play state using a method that changes the application's state. Likewise, it transitions from Play to Stop using a different transition and method to make it happen.
An interface holds the transitions, and each state implements the transitions as methods that are unique to that state. Each method is implemented differently depending on the context of its use. For example, the startPlay() method would do one thing in the Stop state and something entirely different the Play state, even though startPlay() is part of both states. To understand and appreciate the value of the SDP, it helps to know something about state machines.
This article begins with a simple two-state application that plays and stops playing an FLV file. It requires only Flash CS3 Professional and Flash Player 9, which you can download from the links below. The initial application introduces the basics of a state machine and the state design pattern. (With a few changes, the applications in this article will work with Adobe Flex 2 as well.)
The application is then expanded into a more robust one using the same state structure and incorporating Flash Media Server 2. This illustrates both the expandability of an application using a design pattern and the process of incorporating Flash Media Server 2 into that design using ActionScript 3.0.
To complete this tutorial you will need to install the following software:
Because ActionScript 3.0 is so new, any experience you have with Flex 2 will be helpful, but a good grounding in ActionScript 2.0 should be sufficient to understand the structures.
William B. Sanders is currently a professor of Sociology and Multimedia Web Design and Development at the University of Hartford in West Hartford, Connecticut, of which he was a founding faculty member. He has published over 40 computer books, including eight on Flash, ActionScript, or Flash Communication Server. Bill founded Sandlight Productions in 1984 as a computer book and software publication company, which has evolved over the past 23 years into an Internet development company specializing in Flash, ActionScript, and Flash Communication Server applications, along with e-business strategies. He has worked with computers ever since his alma mater, the University of California at Santa Barbara, was one of the original four nodes on ARPANET, along with UCLA, Stanford University, and the University of Utah. When he's not writing, he updates his websites at sandlight.com and mwd.hartford.edu. Bill recently published an instructional video on using Flash Media Server 2 for authorized training provider Train Simple, a new book ActionScript 3.0 Design Patterns (O'Reilly, 2007), and an O'Reilly Short Cut, ActionScript 3.0 Programming: Overview, Getting Started and Examples of New Concepts (O'Reilly, 2007).