Contents

Views 11629 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
417 Develop [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 file hooni 2003.04.23 7805
416 Develop [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 file hooni 2013.04.23 6822
415 Develop [c] 하노이탑 - 재귀함수 hooni 2003.04.23 9174
414 Develop [c] 플러드 필링 (flood filling) 알고리즘.. file hooni 2013.04.23 7332
413 Develop [c] 프로세스간의 통신(파이프) hooni 2003.04.23 6912
412 Develop [c] 프로세스 정보 출력하기.. file hooni 2003.04.23 6938
411 Develop [c] 프로세스 검사하기 hooni 2013.04.23 8047
410 Develop [c] 프로그램 코드(c/c++)를 html 파일로 변환 file hooni 2013.04.23 7689
409 Develop [c] 프로그래밍의 전반적인 설명 ppt file hooni 2003.04.23 7702
408 Develop [c] 프로그래밍 과제..(colprint) file hooni 2003.04.23 6769
407 Develop [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 8215
406 Develop [c] 포인터와 함수포인터에 대해.. hooni 2003.04.23 8043
Board Pagination Prev 1 ... 59 60 61 62 63 64 65 66 67 68 ... 98 Next
/ 98