Contents

조회 수 11679 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
661 Develop 알고리즘 성능분석 file hooni 2014.06.24 3256
660 Develop [ios] iOS에서 디바이스 종류 알아오기 hooni 2014.05.24 3836
659 Develop [ios] 아이폰 GPS 사용하기 hooni 2014.05.24 4145
658 Develop [ios] 언어, 지역, 국가 설정 가져오기 hooni 2014.05.12 273905
657 Develop [ios] URL 파라미터 파싱~ hooni 2014.05.12 3813
656 Develop [ios] 앱 딜리게이트 얻어오기. (AppDelegate) hooni 2014.05.10 3491
655 Develop [ios] 앱딜리게이트 라이프사이클 (AppDelegate Lifecycle) file hooni 2014.05.09 4625
654 Develop [ios] 애플 앱스토어 IDFA 리뷰 정책 변경 안내 file hooni 2014.05.07 4322
653 Develop [ios] NSString URL Encode/Decode (인코딩/디코딩) hooni 2014.05.02 3572
652 Develop [ios] 커스텀 폰트 사용하기 (Custom Fonts) file hooni 2014.04.30 3274
651 Develop [ios] iOS 의 인앱구매 소개 file hooni 2014.04.29 3967
650 Develop [js] Array.splice() 설명 hooni 2014.04.24 3126
Board Pagination Prev 1 ... 11 12 13 14 15 16 17 18 19 20 ... 71 Next
/ 71