Andrew Trice
Real-World Rich Internet Applications
Tuesday September 12, 2006
Singleton in AS3
I was explaining the singleton design pattern to a coworker today, and used Wikipedia as a source of information. There are examples of different languages on there, but no ActionScript! So, I decided to go ahead and add it. Here's the link to AS3 singleton example on wikipedia.
In case you didn't want to go to wikipedia...
package
{
public class Singleton
{
private static var singleton : Singleton
public static function getInstance() : Singleton
{
if ( singleton == null )
singleton = new Singleton( arguments.callee );
return singleton;
}
//NOTE: AS3 does not allow for private or protected constructors
public function Singleton( caller : Function = null )
{
if( caller != Singleton.getInstance )
throw new Error ("Singleton is a singleton class, use getInstance() instead");
if ( Singleton.singleton != null )
throw new Error( "Only one Singleton instance should be instantiated" );
//put instantiation code here
}
}
}
Posted by andrewtrice | Sep 12 2006, 05:19:13 PM EDT
Permalink






