ActionScript 3 Design Patterns excerpt: Factory method and MVC



by William B. Sanders

William B. Sanders


10 September 2007

Page tools
Share on Facebook
Share on Twitter
Share on LinkedIn


User level



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, 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 Design Patterns include:

  • Key features of ActionScript 3 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 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:


More Like This