Contents

Views 12544 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
421 Develop [c] 함수 목록과 예제 소스 파일 file hooni 2013.04.23 8479
420 Develop [c] 한글 문자열 출력 hooni 2015.11.10 3128
419 Develop [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 file hooni 2003.04.23 9082
418 Develop [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 file hooni 2013.04.23 8073
417 Develop [c] 하노이탑 - 재귀함수 hooni 2003.04.23 10704
416 Develop [c] 플러드 필링 (flood filling) 알고리즘.. file hooni 2013.04.23 8570
415 Develop [c] 프로세스간의 통신(파이프) hooni 2003.04.23 7972
414 Develop [c] 프로세스 정보 출력하기.. file hooni 2003.04.23 8846
413 Develop [c] 프로세스 검사하기 hooni 2013.04.23 11659
412 Develop [c] 프로그램 코드(c/c++)를 html 파일로 변환 file hooni 2013.04.23 8740
411 Develop [c] 프로그래밍의 전반적인 설명 ppt file hooni 2003.04.23 9371
410 Develop [c] 프로그래밍 과제..(colprint) file hooni 2003.04.23 8513
Board Pagination Prev 1 ... 59 60 61 62 63 64 65 66 67 68 ... 99 Next
/ 99