Nov 27, 2009
Singleton pattern with static initializer
I’m going to start posting code extracts ![]()
Maybe someone will find this helpful.
package
{
/**
* ...
* @author Dwayne Bull
* @website http://www.dwaynebull.co.uk
* ...
*/
public final class SingletonExample
{
private namespace enforcer = "stop";
enforcer static var accessable : Boolean = false;
private static var instance : SingletonExample = null;
{
enforcer::accessable = true;
instance = new SingletonExample();
enforcer::accessable = false;
}
public static function getInstance():SingletonExample
{
return instance
}
public function SingletonExample()
{
if (!enforcer::accessable) throw new Error("SingletonExample() - Singleton, please use getInstance()");
}
}
}
Thanks also to gskinner for the idea.