Contents

조회 수 12548 댓글 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) {
    }
}


?

  1. [java] 마우스 버튼 테스트.. swing..

    Date2013.04.23 CategoryDevelop Byhooni Views9109
    Read More
  2. [java] 메모패드.. 스윙(swing)으로..

    Date2013.04.23 CategoryDevelop Byhooni Views9370
    Read More
  3. [java] 스윙(swing) 인터페이스 이용해서 만든 구구단.. ㅋㅋ

    Date2003.04.23 CategoryDevelop Byhooni Views8177
    Read More
  4. [java] 스윙(swing)버튼 테스트 ㅋㅋ

    Date2013.04.23 CategoryDevelop Byhooni Views10311
    Read More
  5. [java] 스트러츠(Struts) 세팅 ㅋㅋ

    Date2013.04.23 CategoryDevelop Byhooni Views44338
    Read More
  6. [java] 에디터.. swing 사용

    Date2013.04.23 CategoryDevelop Byhooni Views7506
    Read More
  7. [java] 입출력 스트림 1부 (문자)

    Date2013.04.23 CategoryDevelop Byhooni Views18154
    Read More
  8. [java] 입출력 스트림 2부 (바이트)

    Date2013.04.23 CategoryDevelop Byhooni Views11261
    Read More
  9. [java] 입출력 스트림 3부 (오브젝트)

    Date2013.04.23 CategoryDevelop Byhooni Views19896
    Read More
  10. [java] 채팅 프로그램.. swing 사용..

    Date2013.04.23 CategoryDevelop Byhooni Views9339
    Read More
  11. [java] 채팅창 처럼.. swing..

    Date2013.04.23 CategoryDevelop Byhooni Views10000
    Read More
  12. [java] 채팅창 처럼2.. swing..

    Date2013.04.23 CategoryDevelop Byhooni Views7579
    Read More
Board Pagination Prev 1 ... 50 51 52 53 54 55 56 57 58 59 ... 99 Next
/ 99