Contents

조회 수 11605 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
661 Develop [js] 실행되는 디렉토리 확인하는 스크립트.. hooni 2013.04.23 6766
660 Develop [js] 스타크래프트(starcraft).. file hooni 2013.04.23 6641
659 Develop [js] 스크롤을 포함한 마우스 위치 찾는 코드 hooni 2003.04.23 8505
658 Develop [js] 스크롤 이벤트 막기 hooni 2015.04.14 1013
657 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 6798
656 Develop [js] 순환참조에 의한 메모리 누수 관련 file hooni 2013.12.17 10846
655 Develop [js] 수학 공식을 제공하는 Math 객체 hooni 2013.04.23 15415
654 Develop [js] 수명체크 프로그램 ㅋㅋ file hooni 2013.04.23 6840
653 Develop [js] 셀렉트박스(select)의 옵션(option) 동적으로 추가/제거 file hooni 2013.04.23 8060
652 Develop [js] 서서히 나타나는 화면.. ㅋㅋ hooni 2013.04.23 6196
651 Develop [js] 새로고침(refresh)방법과 다른 페이지 바꾸기.. hooni 2003.04.23 6521
650 Develop [js] 사진첩에 쓸 내용 - 마우스 오버로 바꾸기 hooni 2013.04.23 6341
Board Pagination Prev 1 ... 11 12 13 14 15 16 17 18 19 20 ... 71 Next
/ 71