Develop
2013.11.18 17:11
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
조회 수 11603 댓글 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) { } }
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
369 | System/OS | [windows] 인터넷 익스플로러(IE) 도구모음 표시줄에 아이콘 추가 | hooni | 2013.04.23 | 18219 |
368 | System/OS | [windows] 종료, 재시작, 로그아웃 아이콘 만들기 | hooni | 2013.04.23 | 18103 |
367 | System/OS | [router] 설정과 기본 명령어들 모음 | hooni | 2013.04.23 | 15671 |
366 | System/OS | NAT와 DHCP에 대한 간단한 설명 | hooni | 2013.04.23 | 18328 |
365 | System/OS | [linux] 간단한 find 명령어 설명(업데이트 해야 함) | hooni | 2013.04.23 | 9111 |
364 | Etc | [flash] 맘에 드는 파이차트 | hooni | 2013.04.23 | 12590 |
363 | Develop | [web] 웹 연동 프로그램 모음.. | hooni | 2013.04.23 | 6960 |
362 | System/OS | [doc] TCP/IP 강의 자료 (html) | hooni | 2013.04.23 | 11171 |
361 | System/OS | [linux] 시스템 데몬 종류와 설명 | hooni | 2013.04.23 | 11139 |
360 | System/OS | [doc] 레드햇 리눅스 메뉴얼 (html버전) | hooni | 2013.04.23 | 10452 |
359 | Etc | [flash] 페이지 이동 (액션스크립트) | hooni | 2013.04.23 | 16263 |
358 | PPT | [ppt] 웜 과제 진행사항(프레임워크 검증환경 구축) 보고 | hooni | 2013.04.23 | 15895 |