Develop
2013.11.18 17:11
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
조회 수 12609 댓글 0
| 첨부 '1' |
|---|
싱글톤 패턴 간단 예제
import java.util.HashMap;
class Singleton {
private static HashMap map = new HashMap();
//private static Logger logger = Logger.getRootLogger();
protected Singleton() {
// Exists only to thwart instantiation
}
public static synchronized Singleton getInstance(String classname) {
Singleton singleton = (Singleton)map.get(classname);
if(singleton != null) {
System.out.println("got singleton from map: " + singleton);
return singleton;
}
try {
singleton = (Singleton)Class.forName(classname).newInstance();
}
catch(ClassNotFoundException cnf) {
System.out.println("Couldn't find class " + classname);
}
catch(InstantiationException ie) {
System.out.println(
"Couldn't instantiate an object of type " + classname);
}
catch(IllegalAccessException ia) {
System.out.println("Couldn't access class " + classname);
}
map.put(classname, singleton);
System.out.println("created singleton: " + singleton);
return singleton;
}
}
class SingletonTest {
public static void main(String[] args) {
}
}
-
선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가)
-
[english] 영어공부 혼자 하기, 인터넷으로 영어공부하기 추천사이트 20선
-
이어서 작업할 내용~
-
[ios] In App Purchase 개발
-
[ios] iOS In App Purchase 코드 부분 샘플 2
-
[ios] iOS In App Purchase 코드 부분 샘플 1
-
[ios] iOS In App Purchase #2 (코드 구현)
-
[ios] iOS In App Purchase #1 (코드 구현 전 웹 설정 작업)
-
[ios] iphone SetDeviceOrientation 화면 강제 회전
-
[js] window.open() 속성 사용 방법
-
[php] php5.3부터는 eregi()대신 preg_match()를 사용
-
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드