Contents

조회 수 11619 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
253 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 6813
252 Develop [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 file hooni 2013.04.23 6807
251 Develop [php] 피코맥스용 검색엔진 ㅋㅋ file hooni 2013.04.23 6806
250 Develop [js] ajax를 이용해 외부문서 불러오기.. hooni 2013.04.23 6803
249 Develop [js] One Time Pad key generatorㅡ.,ㅡ; file hooni 2013.04.23 6801
248 Develop [c] 문자열 컨트롤 함수로 만든 프로그램들.. file hooni 2003.04.23 6791
247 Develop [c] 파일 복사(copy) 프로그램 샘플 소스 file hooni 2003.04.23 6791
246 Develop [js] 실행되는 디렉토리 확인하는 스크립트.. hooni 2013.04.23 6770
245 Develop [js] 한글문서로 된 259가지 자바스크립트 예제파일 file hooni 2013.04.23 6768
244 Develop [pdf] C 국제 표준 문서 file hooni 2013.04.23 6767
243 Develop [c] 시간 계산 하는 프로그램 소스코드 file hooni 2003.04.23 6762
242 Develop [c] 프로그래밍 과제..(colprint) file hooni 2003.04.23 6758
Board Pagination Prev 1 ... 45 46 47 48 49 50 51 52 53 54 ... 71 Next
/ 71