코드 샘플: RuntimeStore API를 사용하여 싱글톤 만들기
다음은 런타임 저장소를 사용하여 싱글톤을 만드는 예입니다. 이 예에서, 정적 variable _instance가 시스템에서 실행되는 각 프로세스에 대해 null로 초기화됩니다. 그러므로 getInstance()는 호출 시마다 _instance 변수를 확인해야 합니다.
단순성을 위해 이 예에서는 고유 ID를 만드는 방법을 예시하지 않습니다.
import net.rim.device.api.system.*;
class MySingleton {
private static MySingleton _instance;
private static final long GUID = 0xab4dd61c5d004c18L;
// constructor
MySingleton() {}
public static MySingleton getInstance() {
if (_instance == null) {
_instance = (MySingleton)RuntimeStore.getRuntimeStore().get(GUID);
if (_instance == null) {
MySingleton singleton = new MySingleton();
RuntimeStore.getRuntimeStore().put(GUID, singleton);
_instance = singleton;
}
}
return _instance;
}
}
다음 주제: 레코드 저장소에서 데이터 저장