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
번호 분류 제목 글쓴이 날짜 조회 수
421 Develop [c] 연산자 우선순위.. ㅋㅋ hooni 2013.04.23 8305
420 Develop [c] 지폰(gphone) 소스.. 수정(암호화) file hooni 2013.04.23 8291
419 Develop [asp] 기본 문법과 제어문 hooni 2013.04.23 8283
418 Develop [c] 신기한 atoi함수(www.game79.net) hooni 2003.04.23 8283
417 Develop [c] 로또(Lotto) 번호 생성기 file hooni 2013.04.23 8279
416 Develop [linux] crond 사용법.. ㅋㅋ hooni 2013.04.23 8270
415 Develop [js] php의 number_format() 함수와 같은.. ㅋㅋ hooni 2013.04.23 8266
414 Develop [js] 한글문서로 된 259가지 자바스크립트 예제파일 file hooni 2013.04.23 8263
413 Develop [pdf] 포인터 문법 정리 (C pointer) file hooni 2013.04.23 8262
412 Develop [linux] 임베디드 리눅스 (embedded linux) file hooni 2013.04.23 8262
411 Develop [c] 게임 AI FSM 테스트 샘플 소스.. 꽤 괜찮은 소스.. file hooni 2013.04.23 8248
410 Develop [c] 문자열 라이브러리 최신버전 file hooni 2003.04.23 8247
Board Pagination Prev 1 ... 59 60 61 62 63 64 65 66 67 68 ... 99 Next
/ 99