AS3.0 Better Singletons
I’ve found a better more robust way of enforcing singletons in AS3.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package { public class Singleton { public static var instance:Singleton; public static function getInstance():Singleton { if( instance == null ) instance = new Singleton( new SingletonEnforcer() ); return instance; } public function Singleton( pvt:SingletonEnforcer ) { // init class } } } internal class SingletonEnforcer{} |
Note: The class “SingletonEnforcer” is actually placed within the same .as file as the Singleton class, but placed outside the package{} parenthesis, therefore, the class “SingletonEnforcer” can only be accessed from within this .as file, so if the Singleton’s constructor is called from anywhere else, you’ll get an error.
