Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created December 26, 2009 06:09
Show Gist options
  • Save rjungemann/263855 to your computer and use it in GitHub Desktop.
Save rjungemann/263855 to your computer and use it in GitHub Desktop.
An example of the Singleton pattern in ActionScript
/*
This code comes from http://www.darronschall.com/weblog/2007/11/actionscript-3-singleton-redux.cfm
*/
// in SingletonExample.as
package com.thefifthcircuit.util {
public class SingletonExample {
public var foo:String;
private static const inst:SingletonExample = new SingletonExample(SingletonLock);
public static function get instance():SingletonExample { return inst; }
public function SingletonExample(lock:Class) {
if(lock != SingletonLock) throw new Error("Invalid Singleton access. Use LoaderSingleton.instance.");
foo = "bar";
}
}
}
// in SingletonLock.as
package com.thefifthcircuit.util {
public class SingletonLock {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment