Posts Tagged ‘Singleton’

Design Patterns: Singleton

May 21, 2010

1). Implementation from Scott Morgan: (ExternalInterfaceBuffer source code)

package …
public class ExternalInterfaceBuffer{

private static var instance:ExternalInterfaceBuffer = new ExternalInterfaceBuffer();
public function ExternalInterfaceBuffer() {
if( instance ) throw new Error( “Singleton and can only be accessed through Singleton.getInstance()” );
}

public static function getInstance():ExternalInterfaceBuffer {
return instance;
}

2). from Darron Schall: Actionscript 3 Singleton Redux

package …

private static const _instance:Model = new Model( SingletonLock )
public function Model( lock:Class )
{
// Verify that the lock is the correct class reference.
if ( lock != SingletonLock ){
throw new Error( “Invalid Singleton access.  Use Model.instance.” );
}
}

class SingletonLock{} // end class

3). Grant Skinner: AS3 Singleton

4). What if you would like to pass some arguments in constructor?

Advertisements