Develop
2013.11.18 17:11
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
조회 수 11602 댓글 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) { } }
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
637 | Develop | [ios] UUID 생성 + Key Chain 연동 | hooni | 2016.05.13 | 4670 |
636 | Develop | [ios] Objective-C 프로퍼티의 strong, weak, assign | hooni | 2014.03.17 | 4687 |
635 | Develop | [ios] UITableView 특정 Row만 Update | hooni | 2014.04.08 | 4780 |
634 | Develop | [android] 안드로이드 동영상 스트리밍 예제 2 | hooni | 2015.01.02 | 4823 |
633 | Develop | [PHP] Mac OS에서 PHP 7 설치하기 | hooni | 2018.05.11 | 4889 |
632 | Develop | [ios] Using protobuf(Protocol Buffers) on iPhone (iOS) | hooni | 2014.03.20 | 4986 |
631 | Develop | [c] 윈도우 API Viewport와 Window | hooni | 2013.04.23 | 5959 |
630 | Develop | [js] 자바스크립트 escape()를 PHP로 받기 | hooni | 2013.04.23 | 5990 |
629 | Develop | [doc] C++언어 문법 설명서 | hooni | 2013.04.23 | 6170 |
628 | Develop | [js] 서서히 나타나는 화면.. ㅋㅋ | hooni | 2013.04.23 | 6196 |
627 | Develop | [java] 에디터.. swing 사용 | hooni | 2013.04.23 | 6300 |
626 | Develop | [c][php] 프로세스정보와 메모리 정보 웹으로 출력하는거.. | hooni | 2013.04.23 | 6331 |