Contents

조회 수 12544 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
877 Develop [unix] 날짜 관련 쉘 명령어 (특정일 또는 +-시간, 날짜 계산된 값) hooni 2013.04.23 15855
876 Develop [ios] iOS In App Purchase 코드 부분 샘플 1 hooni 2013.11.20 15826
875 Develop [ios] iOS In App Purchase #2 (코드 구현) file hooni 2013.11.20 15824
874 Develop [api] 인스타그램에서 최신 이미지 가져오기 (Using Instagram API) 20 file hooni 2018.04.05 15805
873 System/OS [sql] 중복데이터 삭제 쿼리 hooni 2013.04.23 15766
872 Develop [C] C언어의 조건 연산자(Conditional Operator) hooni 2003.04.23 15739
871 Develop [ios] Objective-C에서 SQLite 사용하기.. file hooni 2013.04.23 15612
870 System/OS [linux] 랜카드 2개 설정 & iptables 로 사설 ip.. hooni 2003.04.23 15593
869 System/OS [linux] 리눅스 활용 팁^^ hooni 2003.04.23 15541
868 System/OS [linux] 아파치설치/설정(CGI부분) hooni 2003.04.23 15448
867 PPT [ppt] Magic URLs & Hidden Form Fields 에 대해.. ㅋㅋ file hooni 2013.04.23 15441
866 System/OS [linux] sendmail 설정/사용 hooni 2003.04.23 15425
Board Pagination Prev 1 ... 21 22 23 24 25 26 27 28 29 30 ... 99 Next
/ 99