Contents

조회 수 12552 댓글 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. [android] 가속도 센서를 이용한 흔듦(Shake) 감지

    Date2014.11.04 CategoryDevelop Byhooni Views3071
    Read More
  2. [web] 더 빠른 웹을 위한 프로토콜, 'HTTP/2'

    Date2014.10.20 CategoryDevelop Byhooni Views2667
    Read More
  3. GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스

    Date2014.10.14 CategoryDevelop Byhooni Views2071
    Read More
  4. [js] 좋은 강연자료 & UI 자료

    Date2014.10.06 CategoryDevelop Byhooni Views1569
    Read More
  5. '2014 모바일 개발 트렌드' 발표자료입니다.

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

    Date2014.10.02 CategoryDevelop Byhooni Views1797
    Read More
  7. [js] jQuery 셀 병합

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

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

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

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

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

    Date2014.08.08 CategoryDevelop Byhooni Views3514
    Read More
Board Pagination Prev 1 ... 18 19 20 21 22 23 24 25 26 27 ... 99 Next
/ 99