Contents

조회 수 12552 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
769 Develop [c] 문자열 라이브러리 최신버전 file hooni 2003.04.23 8252
768 Develop [ios] Requesting Location Permissions in iOS file hooni 2018.08.18 8253
767 Develop [linux] 임베디드 리눅스 (embedded linux) file hooni 2013.04.23 8265
766 Develop [js] 2차 잉여.. (Quadratic reciprocity) 계산.. file hooni 2013.04.23 8266
765 Develop [pdf] 포인터 문법 정리 (C pointer) file hooni 2013.04.23 8266
764 Develop [js] 한글문서로 된 259가지 자바스크립트 예제파일 file hooni 2013.04.23 8268
763 Develop [js] php의 number_format() 함수와 같은.. ㅋㅋ hooni 2013.04.23 8269
762 Develop [linux] crond 사용법.. ㅋㅋ hooni 2013.04.23 8273
761 Develop [c] 로또(Lotto) 번호 생성기 file hooni 2013.04.23 8282
760 Develop [asp] 기본 문법과 제어문 hooni 2013.04.23 8289
759 Develop [c] 신기한 atoi함수(www.game79.net) hooni 2003.04.23 8293
758 Develop [c] 지폰(gphone) 소스.. 수정(암호화) file hooni 2013.04.23 8294
Board Pagination Prev 1 ... 30 31 32 33 34 35 36 37 38 39 ... 99 Next
/ 99