Views 12544 Votes 0 Comment 0
Atachment
Attachment '1'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

싱글톤 패턴 간단 예제

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
No. Category Subject Author Date Views
251 Develop [c] 프로세스간의 통신(파이프) hooni 2003.04.23 7972
250 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 7969
249 Develop [c++] 템플릿(Template) 예제 소스.. file hooni 2013.04.23 7969
248 Develop 라이브러리에 대한 설명 (static & dynamic library) hooni 2013.04.23 7966
247 Develop [web] 웹 연동 프로그램 모음.. file hooni 2013.04.23 7961
246 Develop [c++] mfc로 만든 인테리어 수납 시스템(2D기반 설계) file hooni 2013.04.23 7959
245 Develop [c++] template 사용예 hooni 2003.04.23 7958
244 Develop [js] 양원님이 공유해 주신 유료(5$란다ㅋ) 자료 ㅋㅋ secret hooni 2013.04.23 7948
243 Develop [js] 스타크래프트(starcraft).. file hooni 2013.04.23 7944
242 Develop [js] 윈도우 시작버튼처럼 나오는 메뉴 file hooni 2013.04.23 7941
241 Develop [c++] 트리컨트롤 예제1 ㅋㅋ file hooni 2013.04.23 7938
240 Develop [js] 가운데 새창 뜨는 함수와 이벤트 hooni 2003.04.23 7936
239 Develop [c] 소켓의 세가지 동작모드 hooni 2003.04.23 7933
238 Develop [php] 메모장 - 웅지학원 ([c] mysql 백업프로그램 포함) file hooni 2003.04.23 7919
237 Develop [c] 단기과정[01/14] 파일 입출력 file hooni 2003.04.23 7917
236 Develop [c] 메세지 프로그램 (Server - Agent - Client) file hooni 2013.04.23 7910
Board Pagination Prev 1 ... 36 37 38 39 40 ... 53 Next
/ 53