Contents

조회 수 11589 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
813 System/OS [linux] root 전환시 패스워드 없이 su 사용하기 hooni 2013.12.22 12681
812 Database [mysql] error while loading shared libraries: libmysqlclient.so.10: hooni 2003.04.23 12663
811 Develop [c++] Win32 API 기본 출력인 MessageBox() 함수 사용 예제.. file hooni 2013.04.23 12623
810 System/OS [linux] 간단한 NAT 설정 script hooni 2003.04.23 12593
809 Etc [flash] 맘에 드는 파이차트 file hooni 2013.04.23 12590
808 Database [sql] 간단한 SQL 문법 정리 hooni 2003.04.23 12551
807 PPT [ppt] 네오웹 소프트.. 발표 자료 모음 file hooni 2003.04.23 12550
806 System/OS [doc] 피쳐셀렉션(feature selection using..) 발표 자료 file hooni 2013.04.23 12544
805 PPT [doc] 위탁과제 제안서와 발표자료(pdf) file hooni 2013.04.23 12534
804 System/OS [linux] 스케쥴링 순서(nice) 변경하기 hooni 2003.04.23 12532
803 Develop [c] flooding 알고리즘 미로 찾기(도스용) 소스코드 9 file hooni 2003.04.23 12488
802 Develop [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동 file hooni 2014.01.24 12486
Board Pagination Prev 1 ... 26 27 28 29 30 31 32 33 34 35 ... 98 Next
/ 98