Contents

조회 수 12552 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
613 Develop [c++] WinSock2.0 채팅 프로그램 ㅋㅋ file hooni 2013.04.23 9409
612 Develop [c] 베지어 곡선(Bézier curve) 알고리즘 file hooni 2013.04.23 9421
611 Develop [c] 마우스 따라다니는 고양이 - 네코95 (WinAPI) file hooni 2013.04.23 9436
610 Develop [jsp] HelloServlet 출력문 file hooni 2003.04.23 9445
609 Develop [c++] mfc로 만든 현재 디렉토리 읽어오기/세팅하기 (GetCurrentDirectory/SetCurrentDirectory) file hooni 2013.04.23 9452
608 Develop [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 9455
607 Develop [c] 반올림 함수!! ㅋㅋ hooni 2003.04.23 9460
606 Develop [js] 주민번호 생성기.. file hooni 2003.04.23 9471
605 Develop [c] 문자열 처리(문자열 자르기) hooni 2003.04.23 9488
604 Develop [c] 간단한 소켓 프로그래밍 샘플 file hooni 2013.04.23 9493
603 Develop [c] 암호 알고리즘 소스.. file hooni 2013.04.23 9506
602 Develop [jsp][php] 간단한 강좌 자료.. file hooni 2003.04.23 9520
Board Pagination Prev 1 ... 43 44 45 46 47 48 49 50 51 52 ... 99 Next
/ 99