Singleton pattern with getInstance
package {
public class Singleton {
private static var instance:Singleton;
private static var allowInstance:Boolean;
public function Singleton() {
if(!allowInstance) {
throw new Error("Error: use Singleton.getInstance() instead of new keyword");
}
}
public static function getInstance():Singleton {
if(instance == null) {
allowInstance = true;
instance = new Singleton();
trace("Singleton instance created");
allowInstance = false;
} else {
trace("Singleton instance already exists");
}
return instance;
}
public function doSomething():void {
trace("doing something");
}
}
}
Related examples in the same category