17.1 Implementing Singleton in ActionScript 2.0

 <  Day Day Up  >  

The Singleton pattern has a relatively simple implementation, particularly considering its general usefulness . Most of the pattern implementation resides in a class we'll refer to as the Singleton class (i.e., the class being limited to a single instance). The remainder of the implementation involves the external access to the lone Singleton instance.

The Singleton class has four main facets (shown in a generic implementation in Example 17-1):

  • A class property, _instance , that stores the lone instance of the class

  • A private constructor, Singleton ( ) , that creates the lone instance

  • An instance-retrieval method, getInstance( ) , that returns a reference to the lone instance

  • Typical instance methods , such as doSomething( ) , and properties (none shown in Example 17-1) that implement the behavior of the class

Example 17-1 shows the source code for a generic Singleton class. Read it over, then we'll consider each of its features in turn .

Example 17-1. The Singleton class
 class Singleton {   private static var _instance:Singleton = null;   private function Singleton ( ) {   }   public static function getInstance( ):Singleton {     if (Singleton._instance == null) {       Singleton._instance = new Singleton( );     }     return Singleton._instance;   }   public function doSomething ( ):Void {     trace("doSomething( ) was called");   } } 

To store the lone instance of the Singleton class, we create a private , static property, _instance , which we initialize to null . The _instance property must be private so that it cannot be accessed outside of the Singleton class or its subclasses. And it must be static so that it can be accessed by the class method getInstance( ) .

To retrieve the lone instance of the Singleton class, other classes use the class method Singleton.getInstance( ) . When called, the getInstance( ) method checks _instance to see whether an instance of the Singleton class exists already. If the _instance property is null , then no instance has yet been created, so the getInstance( ) method creates one and returns it. If, on the other hand, an instance already exists, the getInstance( ) method simply returns the existing instance without creating a new one.

In our Singleton class example, the constructor is empty. Despite that fact, the constructor definition is extremely important because it specifies that the constructor is private (i.e., cannot be accessed outside the Singleton class or its subclasses). Omitting the constructor altogether causes Flash to implicitly create a public constructor, which would allow multiple Singleton instances to be created. When a class's constructor is private, you can use the new operator to create an instance of the class from within the class or its subclasses; however, external attempts to create an instance of the class using the new operator cause an error. For example, when the following code appears outside the Singleton class (and outside any of its subclasses):

 var s:Singleton = new Singleton( ); 

it causes this error:

 The member is private and cannot be accessed. 

Thus, any class that wishes to create a Singleton instance is forced to use the Singleton.getInstance( ) method, which guarantees that only one Singleton instance is ever created.

Notice that because the Singleton instance is stored in a property of the same class, it is automatically accessible to any other class that can access Singleton . (In theory, we could have made our instance accessible to the application by defining it as a global variable, but that global variable's name might conflict with another global variable.)

Here's how we'd use our Singleton class, somewhere in our application:

 var s:Singleton = Singleton.getInstance( ); s.doSomething( ); 

In some cases, it's not necessary to store the Singleton instance in a variable or property. When we have only temporary need of the Singleton instance, we could reduce the previous code to:

 Singleton.getInstance( ).doSomething( ); 

Now that we've seen how the Singleton pattern is implemented in general, let's look at a specific Singleton implementation example ”the Logger class from Chapter 16.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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