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
-
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.