Singleton

public class Singleton
    {
        private static var _instanceDict :Dictionary = new Dictionary();
        public static function getInstance(type : Class, autoCreation : Boolean = true) : *
        {
            if(!_instanceDict[type] && autoCreation)
            {
                _instanceDict[type] = true;
                _instanceDict[type] = new type();
            }
            return _instanceDict[type];
        }
        public static function assertSingle(type : Class) : void
        {
            var tag : * = _instanceDict[type];

            if(!tag || tag is type)
            {
                throw new Error("can't be instantiated whith new, please use single()");
            }
        }
        public static function hasInstance(type : Class) : Boolean
        {
            return (_instanceDict[type] != null);
        }
    }

posted @ 2013-05-07 16:04  模西的哥哥  阅读(145)  评论(0编辑  收藏  举报