AS3의 Singleton을 구현하는 두 가지 방법

1521 단어 functionnullClass
AS3의 Singleton을 구현하는 두 가지 방법
저자: 유대·Poechant메일박스:zhongchao.ustc#gmail.com (# -> @)
블로그csdn.net/poechant
날짜: April 8th, 20121 구조 함수 랜덤 매개 변수
무작위 수를 이용하여 구조 함수의 매개 변수로 삼아 구조 함수를 임의로 호출할 수 없게 한다.
package {
    class Singleton {

        private static var _instance:Singleton = null;

        // secret known only to this class
        private static const secret:Number = Math.random();

        /**
         * @private
         */
        public function Singleton(enforcer:Number) {
            if (enforcer != secret) {
                throw new Error("Error: use Singleton.instance instead");
            }
        }

        /**
         * Global single instance
         */
        public static function get instance():Singleton {
            if (_instance == null) {
                _instance = new Singleton(secret);
            }
            return _instance;
        }
    }
}

2 사유류
AS3 파일 내부의 패키지 정의는 외부에서 하나의 클래스를 실현하는데 이 클래스는 파일 내의 개인적인 것이다.
package
{
    class Singleton {

        private static var _instance:Singleton = null;

        public function Singleton(enforcer:PrivateClass) {
        }

        public function getInstance():Singleton {
            if (_instance == null) {
                _instance = new Singleton();
            }
            return _instance;   
        }
    }
}

class PrivateClass {
    public PrivateClass() {
    }
}

-
전재는 유대에서 온 CSDN 블로그:blog.csdn.net/poechant
-

좋은 웹페이지 즐겨찾기