/*
* name;
*/
export  class Singleton extends Laya.EventDispatcher
{
    private static _dict:any = {};
    constructor(){
        super();
    }

     /**
     * 获取单例类，若不存在则返回空
     * @param	ref 继承自Singleton的类
     * @return	*
     */
    public static Instance(ref:any):any
    {
        if( ref.key == null )
        {
            throw new Error("getinstance erro");
        }
        return this._dict[ref.key];
    }
    
    /**
     * 获取单例类，若不存在则创建
     * @param	ref		继承自Singleton的类
     * @return	*
     */
    public static getInstanceOrCreate(ref:any):any
    {
        if( ref.key == null )
        {
            throw new Error("getInstanceOrCreate erro");
        }
        this.create(ref);
        return this._dict[ref.key];
    }
    
    /**
   * 创建单例类
   * @param	ref		继承自Singleton的类
   * @return	void
   */
    public static create(ref:any):any
    {
        if( ref.key == null )
        {
            throw new Error("create erro");
        }
        if(this._dict[ref.key] == null) 
        {
            this._dict[ref.key] = new ref();
        }
        return this._dict[ref.key];
    }
    
    /**
         * 删除单例类
         * @param	ref		继承自Singleton的类
         * @return	void
         */
    public static remove(ref:any):void
	{
        var key:string = ref.key;
        if(this._dict[key] != null) 
        {
            this._dict[key].destory();
        }
    }
    
    /**
    * 销毁方法
    */
    public destory():void
    {
        
    }
}