Contents

조회 수 11605 댓글 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. '2014 모바일 개발 트렌드' 발표자료입니다.

    Date2014.10.02 CategoryDevelop Byhooni Views990
    Read More
  2. [ios] iOS 8 개발자가 우선 알아야 할 3가지

    Date2014.10.02 CategoryDevelop Byhooni Views911
    Read More
  3. [js] jQuery 셀 병합

    Date2014.09.23 CategoryDevelop Byhooni Views3503
    Read More
  4. IoT가 만드는 미래와 플랫폼 경쟁력

    Date2014.09.23 CategoryEtc Byhooni Views0
    Read More
  5. SVN(Subversion) 설치와 설정 (sasl 인증 적용 포함)

    Date2014.09.11 CategorySystem/OS Byhooni Views5684
    Read More
  6. [linux] yum 업데이트 시 커널 제외하기

    Date2014.09.11 CategorySystem/OS Byhooni Views1268
    Read More
  7. [ios] Xcode cannot run using the selected device

    Date2014.08.14 CategoryDevelop Byhooni Views1800
    Read More
  8. [ios] Objective-C 에서 자주 사용하는 수학 함수와 유용한 Define

    Date2014.08.08 CategoryDevelop Byhooni Views1869
    Read More
  9. [ios] 카테고리 확장 메소드를 찾지 못하는 경우

    Date2014.08.08 CategoryDevelop Byhooni Views2005
    Read More
  10. [ios] @property의 속성 (strong, weak, copy) 사용 경우

    Date2014.08.08 CategoryDevelop Byhooni Views1668
    Read More
  11. OCB5 Injection 앗싸뵹! ㅋㅋ

    Date2014.07.01 CategoryAlgorithm Byhooni Views812
    Read More
  12. [ios] TextField 특정 문자만 사용하도록 하기

    Date2014.06.30 CategoryDevelop Byhooni Views2688
    Read More
Board Pagination Prev 1 ... 18 19 20 21 22 23 24 25 26 27 ... 98 Next
/ 98