ActionScript3でSingletonクラス
ActionScript3だとコンストラクタはpublic以外指定出来ないみたいで、Singletonクラスを作るのはちょっと面倒くさい。
package
{
import flash.errors.IllegalOperationError;
public class Singleton
{
private static var instance:Singleton = new Singleton();
public function Singleton()
{
if (instance === null) {
instance = this;
} else {
throw new IllegalOperationError("not allowed. use getInstance().");
}
}
public static function getInstance():Singleton
{
return instance;
}
}
}
getInstanceを呼ぶのすら面倒くさい場合はこんなのでも一応動くっぽい。
package
{
public const Singleton:_Singleton = new _Singleton();
}
internal class _Singleton {}