Views 11600 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
» Develop [java] 초간단 싱글톤(Singleton) 패턴 샘플 코드 file hooni 2013.11.18 11600
248 Develop [php] php5.3부터는 eregi()대신 preg_match()를 사용 hooni 2013.11.18 12475
247 Develop [js] window.open() 속성 사용 방법 hooni 2013.11.18 13595
246 Develop [ios] iphone SetDeviceOrientation 화면 강제 회전 hooni 2013.11.20 18465
245 Develop [ios] iOS In App Purchase #1 (코드 구현 전 웹 설정 작업) file hooni 2013.11.20 14515
244 Develop [ios] iOS In App Purchase #2 (코드 구현) file hooni 2013.11.20 13773
243 Develop [ios] iOS In App Purchase 코드 부분 샘플 1 hooni 2013.11.20 11765
242 Develop [ios] iOS In App Purchase 코드 부분 샘플 2 hooni 2013.11.20 11281
241 Develop [ios] In App Purchase 개발 hooni 2013.11.20 9268
240 Develop 이어서 작업할 내용~ secret hooni 2013.11.21 0
239 Develop [js] Closure를 이용해 캡슐화.. hooni 2013.12.16 9594
238 Develop [js] jQuery 코드 작성시 편리한 HTML 템플릿 hooni 2013.12.17 33071
237 Develop [js] jQuery 셀랙터(selector) 요약 hooni 2013.12.17 9418
236 Develop [js] jQuery 배열 루프(each) hooni 2013.12.17 9987
235 Develop [js] 객체 머지.. hooni 2013.12.17 8963
234 Develop [js] jQuery 충돌 회피 hooni 2013.12.17 38313
Board Pagination Prev 1 ... 36 37 38 39 40 ... 53 Next
/ 53