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
번호 분류 제목 글쓴이 날짜 조회 수
369 System/OS [linux] wget 명령 사용 예제 hooni 2020.05.26 1450
368 System/OS [linux] Xwindow/Xmanager 사용 hooni 2003.04.23 13343
367 System/OS [linux] X환경 GNOME에서 KDE로 바꾸는 법.. hooni 2013.04.23 12367
366 System/OS [linux] yum 업데이트 시 커널 제외하기 hooni 2014.09.11 1266
365 System/OS [linux] 간단한 find 명령어 설명(업데이트 해야 함) hooni 2013.04.23 9111
364 System/OS [linux] 간단한 NAT 설정 script hooni 2003.04.23 12608
363 System/OS [linux] 간단한 vi편집기 사용 명령 hooni 2003.04.23 13456
362 System/OS [linux] 계정관리하기(내공쌓기) hooni 2003.04.23 13163
361 System/OS [linux] 기본 명령어 (내공쌓기) hooni 2003.04.23 14333
360 System/OS [linux] 기본적인 설정하기(내공쌓기) hooni 2003.04.23 13994
359 System/OS [linux] 기존 환경설정 저장하면서 커널 컴파일.. hooni 2003.04.23 13469
358 System/OS [linux] 꿀통(honeyd) 설치하기.. 메뉴얼 과정 6 file hooni 2006.04.23 20223
Board Pagination Prev 1 ... 63 64 65 66 67 68 69 70 71 72 ... 98 Next
/ 98