Develop
2013.11.18 17:11
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
조회 수 11605 댓글 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) { } }
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
909 | Develop | '2014 모바일 개발 트렌드' 발표자료입니다. | hooni | 2014.10.02 | 990 |
908 | Develop | [ios] iOS 8 개발자가 우선 알아야 할 3가지 | hooni | 2014.10.02 | 911 |
907 | Develop | [js] jQuery 셀 병합 1 | hooni | 2014.09.23 | 3503 |
906 | Etc | IoT가 만드는 미래와 플랫폼 경쟁력 | hooni | 2014.09.23 | 0 |
905 | System/OS | SVN(Subversion) 설치와 설정 (sasl 인증 적용 포함) | hooni | 2014.09.11 | 5682 |
904 | System/OS | [linux] yum 업데이트 시 커널 제외하기 | hooni | 2014.09.11 | 1268 |
903 | Develop | [ios] Xcode cannot run using the selected device | hooni | 2014.08.14 | 1800 |
902 | Develop | [ios] Objective-C 에서 자주 사용하는 수학 함수와 유용한 Define | hooni | 2014.08.08 | 1869 |
901 | Develop | [ios] 카테고리 확장 메소드를 찾지 못하는 경우 | hooni | 2014.08.08 | 2004 |
900 | Develop | [ios] @property의 속성 (strong, weak, copy) 사용 경우 | hooni | 2014.08.08 | 1667 |
899 | Algorithm | OCB5 Injection 앗싸뵹! ㅋㅋ | hooni | 2014.07.01 | 811 |
898 | Develop | [ios] TextField 특정 문자만 사용하도록 하기 | hooni | 2014.06.30 | 2687 |