Contents

조회 수 11602 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
637 Develop [c] scanf(), printf() 포맷의 형변환 hooni 2003.04.23 10856
636 Develop [c] selec()를 이용한 입출력 다중화 file hooni 2013.04.23 8259
635 Develop [c] SetTimer() & KillTimer() & 일회용 Timer hooni 2013.04.23 9209
634 Develop [c] UCP/TCP 채팅 소스 - 정리해야 함.. file hooni 2003.04.23 7911
633 Develop [c] Unix Domain Socket 을 이용한 IPC hooni 2013.04.23 8019
632 Develop [c] UTF-8을 EUC-KR로 변환.. (iconv) file hooni 2013.04.23 20137
631 Develop [c] vc++ 에서 clrscr(), gotoxy() 함수 사용하기.. hooni 2013.04.23 14254
630 Develop [c] 가변인자 함수(printf와 같은..) hooni 2013.04.23 7179
629 Develop [c] 가위 바위 보 서버, 클라이언트 소스코드 file hooni 2003.04.23 8178
628 Develop [c] 간단한 링크드 리스트(linked list) 자료형 예제.. hooni 2003.04.23 9839
627 Develop [c] 간단한 소켓 프로그래밍 샘플 file hooni 2013.04.23 8162
626 Develop [c] 간단한 순위 루틴.. (정보처리기사) hooni 2003.04.23 6874
Board Pagination Prev 1 ... 13 14 15 16 17 18 19 20 21 22 ... 71 Next
/ 71