LoginSignup
6
5

More than 5 years have passed since last update.

HaxeでSingletonパターンを実装する

Posted at

HaxeでSingletonパターンを実装するメモです。
GOFのパターンに近いです。
自分はどの言語でもこの形式で実装しています。

class Singleton {
    private static var instance=null;
    private static var internallyCalled:Bool = false;

    public function new() {
        if(internallyCalled){
            internallyCalled=false;
        }else{
            throw "Singleton.getInstance()で生成してね。";
        }
    }

    public static function getInstance():Singleton{
        if(Singleton.instance==null){
            internallyCalled = true;
            instance = new Singleton();
        }
        return instance;
    }
}

インスタンス生成はstaticにgetInstanceで。


var singleton=Singleton.getInstance();

TypeScript版をこちらで投稿しています。
http://qiita.com/STYLELAB/items/42426712f1c844e02ef8

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5