Singleton pattern with getInstance : Singleton : Class Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Class » Singleton »

 

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");
    }

  }

}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Class
» Singleton