Develop
2013.11.18 17:11
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
조회 수 12908 댓글 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) {
}
}
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|---|
| » | Develop |
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
|
hooni | 2013.11.18 | 12908 |
| 805 |
[ios] 인앱결제 & 오토레이아웃 관련 강좌
|
hooni | 2013.11.14 | 0 | |
| 804 | Etc | [NFC] 단말기와 서버 통신 내용 | hooni | 2013.11.12 | 12250 |
| 803 | Develop | [python] 파이썬 공부하는 사이트~ | hooni | 2013.11.12 | 12028 |
| 802 | System/OS | [mac] Charlesproxy 간단한 설정 내용~ | hooni | 2013.11.12 | 13549 |
| 801 | Develop | [js] 웹페이지에서 특정 엘리먼트 드래그, 복사, 컨텍스트메뉴, 키보드 막기 | hooni | 2013.11.04 | 35102 |
| 800 | Etc | 티스토리 테이블 html,css 구문 | hooni | 2013.11.03 | 17863 |
| 799 | Develop | [php] XE 에서 php 구문 사용하기 (XE 템플릿에서) | hooni | 2013.10.31 | 21346 |
| 798 | Develop | [ios] 배열(NSArray) 연산과 간단한 애니메이션(split images) | hooni | 2013.10.31 | 53179 |
| 797 | Develop | [ios] Random Thoughts: Rand() vs. arc4random() | hooni | 2013.10.31 | 78803 |
| 796 | System/OS | [linux] 특정 문자열 포함된 파일 찾는 명령어 | hooni | 2013.10.16 | 33540 |
| 795 | Develop | [ios] UIAlertView 초간단 샘플 ㅎㅎ | hooni | 2013.10.14 | 56651 |