Singleton 执行器通过私有实例

在这种方法中,通过静态方法访问单个:

Singleton.getInstance();

要仅强制执行单例的一个实例,私有静态变量将保留实例,而在构造函数中强制实例化实例的任何其他尝试。

package {

public class Singleton {

    /** Singleton instance */
    private static var _instance: Singleton = new Singleton();

    /** Return singleton instance. */
    public static function getInstance():Singleton {
        return _instance;
    }

    /** Constructor as singleton enforcer. */
    public function Singleton() {
        if (_instance)
            throw new Error("Singleton is a singleton and can only be accessed through Singleton.getInstance()");
    }

}
}