Contents

조회 수 12545 댓글 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] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 9449
768 Develop [c] 프로그래밍 과제..(colprint) file hooni 2003.04.23 8516
767 Develop [c] 프로그래밍의 전반적인 설명 ppt file hooni 2003.04.23 9373
766 Develop [c] 프로그램 코드(c/c++)를 html 파일로 변환 file hooni 2013.04.23 8740
765 Develop [c] 프로세스 검사하기 hooni 2013.04.23 11660
764 Develop [c] 프로세스 정보 출력하기.. file hooni 2003.04.23 8848
763 Develop [c] 프로세스간의 통신(파이프) hooni 2003.04.23 7991
762 Develop [c] 플러드 필링 (flood filling) 알고리즘.. file hooni 2013.04.23 8570
761 Develop [c] 하노이탑 - 재귀함수 hooni 2003.04.23 10706
760 Develop [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 file hooni 2013.04.23 8074
759 Develop [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 file hooni 2003.04.23 9082
758 Develop [c] 한글 문자열 출력 hooni 2015.11.10 3136
Board Pagination Prev 1 ... 30 31 32 33 34 35 36 37 38 39 ... 99 Next
/ 99