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
번호 분류 제목 글쓴이 날짜 조회 수
255 Develop 도메인 관련 솔루션 분석할 거.. ㅋㄷ file hooni 2013.04.23 7975
254 Develop [js] 윤동이가 만든 영어 학습(?) 프로그램 file hooni 2013.04.23 7974
253 Develop [php] 빔 프로젝터 예약 프로그램.. ㅋㅋ file hooni 2013.04.23 7974
252 Develop [c] 프로세스간의 통신(파이프) hooni 2003.04.23 7972
251 Develop [php] 논문 관리 프로그램.. ㅋㅋ file hooni 2013.04.23 7972
250 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 7969
249 Develop [c++] 템플릿(Template) 예제 소스.. file hooni 2013.04.23 7968
248 Develop 라이브러리에 대한 설명 (static & dynamic library) hooni 2013.04.23 7966
247 Develop [web] 웹 연동 프로그램 모음.. file hooni 2013.04.23 7961
246 Develop [c++] mfc로 만든 인테리어 수납 시스템(2D기반 설계) file hooni 2013.04.23 7959
245 Develop [c++] template 사용예 hooni 2003.04.23 7957
244 Develop [js] 양원님이 공유해 주신 유료(5$란다ㅋ) 자료 ㅋㅋ secret hooni 2013.04.23 7948
Board Pagination Prev 1 ... 45 46 47 48 49 50 51 52 53 54 ... 71 Next
/ 71