Contents

조회 수 11632 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
601 Develop [c] 농구팀 점수 산출 프로그램 소스 file hooni 2003.04.23 7413
600 Develop [c] 다중연결 서버 만들기 #1 - fork() 사용 file hooni 2013.04.23 12948
599 Develop [c] 다중연결 서버 만들기 #2 - select() 사용 file hooni 2013.04.23 9378
598 Develop [c] 다중연결 서버 만들기 #3 - poll() 사용 file hooni 2013.04.23 6369
597 Develop [c] 다중연결 서버 만들기 #4 - thread 사용 file hooni 2013.04.23 23750
596 Develop [c] 단기과정[01/06] sizeof, 실수표현, 메모리, 연산자 hooni 2003.04.23 6961
595 Develop [c] 단기과정[01/07] 제어문, 피보나치수열 hooni 2003.04.23 7439
594 Develop [c] 단기과정[01/08] (다차원 + 배열)포인터, void 포인터 hooni 2003.04.23 7803
593 Develop [c] 단기과정[01/08] 과제.. 파스칼 삼각형 file hooni 2003.04.23 7296
592 Develop [c] 단기과정[01/08] 배열, 포인터 hooni 2003.04.23 7267
591 Develop [c] 단기과정[01/10] 과제.. swap(any data, ..) file hooni 2003.04.23 6933
590 Develop [c] 단기과정[01/14] 파일 입출력 file hooni 2003.04.23 6862
Board Pagination Prev 1 ... 16 17 18 19 20 21 22 23 24 25 ... 71 Next
/ 71