Contents

조회 수 11600 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
765 Develop [php] 초간단 웹 카운터.. file hooni 2003.04.23 7063
764 Develop [js] 2차 잉여.. (Quadratic reciprocity) 계산.. file hooni 2013.04.23 7075
763 Develop [asp] 기본 문법과 제어문 hooni 2013.04.23 7076
762 Develop [c] 단기과정[01/14] 피보나치, 파스칼.. 파일입출력 file hooni 2003.04.23 7081
761 Develop [c] 베이지언(Bayesian) 패턴인식 과제 ㅋㅋ file hooni 2013.04.23 7095
760 Develop [pdf] 포인터 문법 정리 (C pointer) file hooni 2013.04.23 7106
759 Develop [c] 테트리스3D (Tetris 3D) file hooni 2013.04.23 7107
758 Develop [c++] 인라인 함수에 대한 설명 hooni 2013.04.23 7110
757 Develop [php] 현재에서 일정한 날짜 더하기.. hooni 2013.04.23 7112
756 Develop [c] 문자열 뒤집기 초간단 샘플 코드 ㅎㅎ hooni 2013.04.23 7116
755 Develop [ajax] 이벤트 코드 생성기 작업중.. ㅋㅋ file hooni 2013.04.23 7117
754 Develop [js] 여러가지 트리(tree) 모음.. ㅋㅋ file hooni 2013.04.23 7120
Board Pagination Prev 1 ... 30 31 32 33 34 35 36 37 38 39 ... 98 Next
/ 98