Contents

조회 수 11602 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
369 Develop [js] 사라지는 브라우저 hooni 2003.04.23 6857
368 Develop [c] 문자열 str_shift 예제.. file hooni 2013.04.23 6843
367 Develop [pdf] 윈도우즈 95 시스템 프로그래밍(Windows 95 system programming) file hooni 2013.04.23 6842
366 Develop [ajax] 샘플 코드와 한글처리에 대한 간단한 설명 hooni 2013.04.23 6842
365 Develop [js] get방식, url이후 모두 그대로 읽어오기.. hooni 2013.04.23 6841
364 Develop [c++] 프리렉(freelec) 예제 자료.. ㅋㄷ file hooni 2013.04.23 6839
363 Develop [js] 수명체크 프로그램 ㅋㅋ file hooni 2013.04.23 6839
362 Develop [c] 웹 메모장.. ㅋㅋ file hooni 2013.04.23 6827
361 Develop [c] 단기과정[01/14] 파일 입출력 file hooni 2003.04.23 6821
360 Develop [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 file hooni 2013.04.23 6800
359 Develop [js] ajax를 이용해 외부문서 불러오기.. hooni 2013.04.23 6799
358 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 6795
Board Pagination Prev 1 ... 63 64 65 66 67 68 69 70 71 72 ... 98 Next
/ 98