Contents

조회 수 12544 댓글 0
Atachment
첨부 '1'
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

싱글톤 패턴 간단 예제

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) {
    }
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
817 System/OS [linux] 한글 URL 인식할 수 있게 아파치(Apache) 설정 (mod_url.c 설치) hooni 2013.04.23 13966
816 System/OS [linux] ipfwadm를 이용한 패킷필터링(구버전) hooni 2003.04.23 13950
815 System/OS [linux] ProFTPD 타임아웃 설정 hooni 2003.04.23 13950
814 Develop [c] 다중연결 서버 만들기 #1 - fork() 사용 file hooni 2013.04.23 13943
813 System/OS [linux] 아파치설치/설정 - SSI(Server Side Include) hooni 2003.04.23 13940
812 Etc 선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가) hooni 2013.12.04 13904
811 Develop [c++] 자료구조(링크리스트,스택,큐)와 후위 표기 계산기 샘플 ㅋㅋ 4 file hooni 2013.04.23 13891
810 Develop [php] 심플한 게시판 ㅋㅋ 1 file hooni 2013.04.23 13859
809 System/OS [linux] 콘솔 기본언어 설정 방법 hooni 2013.04.23 13849
808 System/OS [linux] 아파치 설치/설정(proxy) hooni 2003.04.23 13849
807 Develop [js] AngularJS를 소개합니다. file hooni 2014.01.06 13845
806 System/OS [linux] Proftpd 설치 가이드 hooni 2003.04.23 13845
Board Pagination Prev 1 ... 26 27 28 29 30 31 32 33 34 35 ... 99 Next
/ 99