조회 수 12548 추천 수 0 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
697 Develop [js] JSON 컨트롤.. 재귀호출로 값 출력하기 hooni 2013.04.23 29494
696 Develop [js] jQuery, Javascript 모바일(스마트폰) 판단하는 방법 hooni 2015.04.26 3805
695 Develop [js] jQuery 코드 작성시 편리한 HTML 템플릿 hooni 2013.12.17 34235
694 Develop [js] jQuery 치트 시트 hooni 2013.12.18 37215
693 Develop [js] jQuery 충돌 회피 hooni 2013.12.17 39468
692 Develop [js] jQuery 셀랙터(selector) 요약 hooni 2013.12.17 10272
691 Develop [js] jQuery 셀 병합 1 file hooni 2014.09.23 4513
690 Develop [js] jQuery 관련 사이트 링크 모음 hooni 2013.04.23 19376
689 Develop [js] jQuery 관련 문서 ㅎㅎ file hooni 2013.04.23 32004
688 Develop [js] jQuery plugin 요약 hooni 2013.12.20 11590
687 Develop [js] jQuery 배열 루프(each) hooni 2013.12.17 11102
686 Develop [js] jQjuery $ 활용 hooni 2013.12.17 10194
685 Develop [js] Javascript로 만든 포트리스 (2010) 5 file hooni 2017.03.03 5495
684 Develop [js] IE에서 인쇄 설정 팁 hooni 2013.04.23 11948
683 Develop [js] get방식, url이후 모두 그대로 읽어오기.. hooni 2013.04.23 7985
682 Develop [js] e.stopPropagation() VS e.preventDefault () file hooni 2015.04.14 1758
Board Pagination Prev 1 ... 29 30 31 32 33 ... 74 Next
/ 74