About

<#TEMPLATE_INCLUDE_NINEPAGE_ABOUTME#>
  • Nov

    20

    AS3 单例模式

    • 0 Comments
    • Flash Platform
    package
    {
        public class Instance
        {
            //是否已经有单例对象
            static private var _hasInstance:Boolean = true;

            //单例
            static private var _instance:Instance;

            public function Instance()
            {
                if(_hasInstance)
                {
                    throw new Error("这是一个单例,须通过 getInstance() 获取实例");
                }
            }

            static public function getInstance():Instance
            {
                if (!_instance)
                {
                    _hasInstance=false;
                    _instance = new Instance();
                    _hasInstance=true;
                }
                return _instance;
            }
        }
    }