조회 수 11629 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
249 Develop [php] 자바스크립트 개판 만들기.. file hooni 2013.04.23 7674
248 Develop [js] 자바스크립트로 만든 게임 file hooni 2013.04.23 8404
247 Develop [js] 윤동이가 만든 영어 학습(?) 프로그램 file hooni 2013.04.23 6475
246 Develop [js] 밀리터리 프로그램(전역일 계산) 7 file hooni 2013.04.23 8925
245 Develop [js] 빈도우즈(bindows96) file hooni 2013.04.23 7391
244 Develop [js] 후리자(영규) 스타일들.. file hooni 2013.04.23 7174
243 Develop [js] 여러가지 트리(tree) 모음.. ㅋㅋ file hooni 2013.04.23 7134
242 Develop [js] IE에서 인쇄 설정 팁 hooni 2013.04.23 10913
241 Develop [web] 웹 연동 프로그램 모음.. file hooni 2013.04.23 6980
240 Develop [c] 웅지학원 NAT를 소스코드로.. file hooni 2013.04.23 6623
239 Develop [c] 소켓 스트림 서버/클라이언트 (UDP) file hooni 2013.04.23 7610
238 Develop [c++] 마방진 소스코드 file hooni 2013.04.23 9569
237 Develop [c] 민수형 소스(도메인소켓포함) file hooni 2013.04.23 6542
236 Develop [c] GD라이브러리(jpeg)를 사용한 웹 카운터 샘플 hooni 2013.04.23 15125
235 Develop JSON, BSON 변환 file hooni 2013.04.23 11866
234 Develop [js] 2차 잉여.. (Quadratic reciprocity) 계산.. file hooni 2013.04.23 7089
Board Pagination Prev 1 ... 36 37 38 39 40 ... 53 Next
/ 53