Design Patterns: Singleton

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

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: