« インスタンス化させないために | メイン | Abstract »

2007年7月31日

[as3] AS3でSingleton

Singleton メモ

package
{
  public class Singleton
  {
    private static var instance:Singleton;
    
    function Singleton(caller:Function=null):void
    {
      if( caller != createInstance ) throw new ArgumentError("Cannot user access constructor.");
      
      trace("create new instance.");
    }
    
    private static function createInstance():Singleton
    {
      return new Singleton(arguments.callee);
    }
    
    public static function getInstance():Singleton
    {
      if( instance == null ){
        instance = createInstance();
      }
      return instance;
    }
  }
}

投稿者 michi : 2007年7月31日 13:28

« インスタンス化させないために | メイン | Abstract »

トラックバック

このエントリーのトラックバックURL:
http://system.seyself.com/mt-tb.cgi/91

コメント

コメントしてください




保存しますか?