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
번호 분류 제목 글쓴이 날짜 조회 수
609 Develop [linux] 날짜나 파일의 내용으로 검색하는 방법(find 명령 사용) hooni 2013.04.23 8123
608 Develop [swift] NotificationCenter 간단 예제 file hooni 2021.01.27 8123
607 Develop Mac OS 에 Jenkins 설치하기 (Homebrew) 2 file hooni 2017.03.15 8140
606 Develop [c#] 본현이형 논문 자료 (HIDS)ㅋㅋ file hooni 2013.04.23 8142
605 Develop [c] GD라이브러리 설치 테스트 소스 file hooni 2013.04.23 8144
604 Develop [js] 주민번호 생성기.. file hooni 2003.04.23 8145
603 Develop [jsp] 페이지에 한글이 정상적으로 보이지 않을 때.. hooni 2013.04.23 8163
602 Develop [c] 간단한 소켓 프로그래밍 샘플 file hooni 2013.04.23 8164
601 Develop [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 8179
600 Develop [c] 가위 바위 보 서버, 클라이언트 소스코드 file hooni 2003.04.23 8180
599 Develop [vb] 64bit RSA 프로그램 소스 ㅋㅋ file hooni 2013.04.23 8193
598 Develop [c] 스토리지 클래스(변수) hooni 2003.04.23 8198
Board Pagination Prev 1 ... 43 44 45 46 47 48 49 50 51 52 ... 98 Next
/ 98