Accessibility

ActionScript Article

 

ActionScript 3.0 Design Patterns excerpt: Factory method and MVC


ActionScript 3.0 Design Patterns excerpt: Factory method and MVC

William B. Sanders

sandlight.com

Chadima Cumaranatunge

 

Created:
10 September 2007
User Level:
Intermediate, Advanced
Products:
Flex
Flash

Now that ActionScript is reengineered from top to bottom as a true object-oriented programming (OOP) language, reusable design patterns are an ideal way to solve common problems in Flash and Flex applications. If you're an experienced Flash or Flex developer ready to tackle sophisticated programming techniques with ActionScript 3.0, this hands-on introduction to design patterns takes you step by step through the process. William B. Sanders and Chadima Cumaranatunge show you various types of design patterns and construct small abstract examples before trying your hand at building full-fledged working applications outlined in the book.

Topics in ActionScript 3.0 Design Patterns include:

  • Key features of ActionScript 3.0 and why it became an OOP language
  • OOP characteristics, such as classes, abstraction, inheritance, and polymorphism
  • Benefits of using design patterns
  • Creational patterns, including Factory and Singleton patterns
  • Structural patterns, including Decorator, Adapter, and Composite patterns
  • Behavioral patterns, including Command, Observer, Strategy, and State patterns
  • Multiple design patterns, including Model-View-Controller and Symmetric Proxy designs

During the course of the book, you'll work with examples of increasing complexity, such as an e-business application with service options that users can select, an interface for selecting a class of products and individual products in each class, an action game application, a video record and playback application, and many more.

The printed book is available through most major online and retail bookstores worldwide. For more information visit the O'Reilly store.

ActionScript 3.0 Design Patterns © 2007 William B. Sanders and Chandima Cumaranatunge. Reproduced by permission of O'Reilly Media Inc. All rights reserved.

Factory method pattern

One of the most common statements in object-oriented programming (OOP) uses the new keyword to instantiate objects from concrete classes. ActionScript applications that have multiple classes can have an abundance of code that looks like the following:

public class Client
{
  public function doSomething( )
  {
     var object:Object = new Product( );
     object.manipulate( );
  }
}

The Client class creates a new instance of the Product class and assigns it to the variable object. There's nothing wrong with this code, but it does create a coupling or dependency between the Client and Product classes. Simply put, the Client class depends on the Product class to function properly. Any changes to the Product class in terms of class name changes or change in the number of parameters passed to it will require changes in the Client class as well. This situation is exacerbated if multiple clients use the Product class, and requires changing code in multiple locations.

The solution to this common problem is to loosen the tight coupling between the client and the concrete classes it uses. This is where the factory method pattern offers a robust solution. It introduces an intermediary between the client and the concrete class. The intermediary is called a creator class. It allows the client to access objects without specifying the exact class of object that will be created. This is accomplished by delegating object creation to a separate method in the creator called a factory. The primary purpose of the factory method is to instantiate objects and return them.

Download the complete chapter:

Model-View-Controller pattern

The Model-View-Controller (MVC) is a compound pattern, or multiple patterns working together to create complex applications. The MVC pattern is most commonly used to create interfaces for software applications—and, as the name implies, consists of three elements:

  • Model: Contains the application data and logic to manage the state of the application
  • View: Presents the user interface and the state of the application onscreen
  • Controller: Handles user input to change the state of the application

The power of the MVC pattern can be directly attributed to the separation of the three elements without overlap in each of their responsibilities.

Download the complete chapter:

About the authors

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).

Dr. Chadima Cumaranatunge is an assistant professor of Interactive Information Technology at the University of Hartford. He teaches an introduction to the IIT major, covering Flash and some ActionScript, a gaming course using Flash and ActionScript as well as educational technology courses in the Education, Nursing, and Health Professions College. Recently he received a grant to teach an experimental course in robotics.