Contents

Views 12544 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
639 Develop [js] 사진첩에 쓸 내용 - 마우스 오버로 바꾸기 hooni 2013.04.23 7436
638 Develop [doc] C언어 문법 설명서 file hooni 2013.04.23 7437
637 Develop [c] 다중연결 서버 만들기 #3 - poll() 사용 file hooni 2013.04.23 7459
636 Develop [c] 민수형 소스(도메인소켓포함) file hooni 2013.04.23 7491
635 Develop [ios] FlckrFeed Example App (Swift) file hooni 2016.11.27 7498
634 Develop [java] 에디터.. swing 사용 file hooni 2013.04.23 7505
633 Develop [c] alarm()함수 설명과 간단한 예제 file hooni 2013.04.23 7556
632 Develop [java] 채팅창 처럼2.. swing.. file hooni 2013.04.23 7577
631 Develop [js] 새로고침(refresh)방법과 다른 페이지 바꾸기.. hooni 2003.04.23 7603
630 Develop [php] 3 Ways to Detect Mobile or Desktop in PHP file hooni 2020.01.28 7606
629 Develop [android] dp, px 서로 변환 hooni 2016.10.21 7609
628 Develop [c] 네트워크 보안 프로그래밍 과제 (Server,Agent,Client) file hooni 2013.04.23 7612
Board Pagination Prev 1 ... 13 14 15 16 17 18 19 20 21 22 ... 71 Next
/ 71