Contents

조회 수 12544 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
219 Develop [c#]뉴 툴바 개인적으로 만든거.. (new) secret hooni 2013.04.23 7724
218 Develop [c][php] 프로세스정보와 메모리 정보 웹으로 출력하는거.. file hooni 2013.04.23 7660
217 Develop [c#]업글 뉴 툴바 개인적으로 만든거.. (new) ㅋㅋ secret hooni 2013.04.23 7651
216 Develop [android] dp, px 서로 변환 hooni 2016.10.21 7603
215 Develop [js] 새로고침(refresh)방법과 다른 페이지 바꾸기.. hooni 2003.04.23 7602
214 Develop [c] 네트워크 보안 프로그래밍 과제 (Server,Agent,Client) file hooni 2013.04.23 7601
213 Develop [php] 3 Ways to Detect Mobile or Desktop in PHP file hooni 2020.01.28 7601
212 Develop [java] 채팅창 처럼2.. swing.. file hooni 2013.04.23 7576
211 Develop [c] alarm()함수 설명과 간단한 예제 file hooni 2013.04.23 7554
210 Develop [java] 에디터.. swing 사용 file hooni 2013.04.23 7504
209 Develop [ios] FlckrFeed Example App (Swift) file hooni 2016.11.27 7495
208 Develop [c] 민수형 소스(도메인소켓포함) file hooni 2013.04.23 7490
Board Pagination Prev 1 ... 48 49 50 51 52 53 54 55 56 57 ... 71 Next
/ 71