Contents

Views 11639 Comment 0
Atachment
Attachment '1'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

싱글톤 패턴 간단 예제

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
No. Category Subject Author Date Views
601 Develop [js] IE에서 인쇄 설정 팁 hooni 2013.04.23 10924
600 Develop [ios] 한샘 카달로그 앱 소스 (아이폰용) secret hooni 2013.04.23 10922
599 Develop [c] scanf(), printf() 포맷의 형변환 hooni 2003.04.23 10904
598 Develop [python] 파이썬 공부하는 사이트~ hooni 2013.11.12 10898
597 Develop [unix] 로그파일 정리 쉘스크립트 hooni 2014.02.19 10886
596 Develop [js] 순환참조에 의한 메모리 누수 관련 file hooni 2013.12.17 10866
595 Develop [c] 거리와 각도를 입력받아서 좌표로 변환 file hooni 2013.04.23 10848
594 Develop [php] XE 관리자 IP대역 설정 오류 해결법 hooni 2014.02.10 10825
593 Develop [c++] 가짜 인증서(하나은행) 프로그램 file hooni 2013.04.23 10787
592 Develop [c++] p.118 확인학습 5번 hooni 2003.04.23 10777
591 Develop [js] jQuery plugin 요약 hooni 2013.12.20 10754
590 Develop [c++]현승이가 보내준 암호화 모듈 AES라인델.. file hooni 2003.04.23 10745
Board Pagination Prev 1 ... 16 17 18 19 20 21 22 23 24 25 ... 71 Next
/ 71