Using the Singleton design pattern

A common way to use class members is with the Singleton design pattern. A design pattern defines a formal approach for structuring your code. Typically, you might structure a design pattern as a solution for a common programming problem. There are many established design patterns, such as Singleton. The Singleton design pattern makes sure that a class has only one instance and provides a way of globally accessing the instance. For detailed information on the Singleton design pattern, see www.adobe.com/devnet/coldfusion/articles/design_patterns.html.

Often you encounter situations when you need exactly one object of a particular type in a system. For example, in a chess game, there is only one chessboard, and in a country, there is only one capital city. Even though there is only one object, you should encapsulate the functionality of this object in a class. However, you might need to manage and access the one instance of that object. Using a global variable is one way to do this, but global variables are not desirable for most projects. A better approach is to make the class manage the single instance of the object itself using class members. The following example shows a typical Singleton design pattern usage, where the Singleton instance is created only once.

To use the Singleton design pattern:

  1. Select File > New and then select ActionScript File. Save the document as Singleton.as.
  2. Type the following ActionScript code into the Script window:
    /**
        Singleton class
        author: John Doe
        version: 0.53
        modified: 6/24/2008
        copyright: Adobe Systems Incorporated
    */
    
    class Singleton {
        private static var instance:Singleton = null;
        public function trackChanges():Void {
            trace("tracking changes.");
        }
        public static function getInstance():Singleton {
            if (Singleton.instance == null) {
                trace("creating new Singleton.");
                Singleton.instance = new Singleton();
            }
            return Singleton.instance;
        }
    }
    
  3. Save the Singleton.as document.
  4. Select File > New and then select Flash Document to create a new FLA file, and save it as singleton_test.fla in the same directory as you saved the Singleton class file.
  5. Type the following ActionScript code into Frame 1 of the Timeline:
    Singleton.getInstance().trackChanges(); // tracking changes.
    
    var s:Singleton = Singleton.getInstance(); // tracking changes.
    s.trackChanges();
    
  6. Save the Flash document.
  7. Select Control > Test Movie to test the document.

The Singleton object is not created until you need it--that is, until some other code asks for it by calling the getInstance() method. This is typically called lazy creation, and it can help make your code more efficient in many circumstances.

Remember not to use too few or too many class files for your application, because doing so can lead to poorly designed class files, which are not beneficial to the application's performance or to your workflow. You should always attempt to use class files instead of placing code in other places (such as timelines); however, avoid creating many classes that have only a small amount of functionality or only a few classes that handle a lot of functionality. Both of these scenarios might indicate poor design.