김르르
Apr 8, 2021

*Singleton Pattern

- 실행 중인 JVM에서 단 하나의 객체만 만들고 사용하는 design pattern.

- 참조하는 속도가 빠르다.

- 메모리를 절약할 수 있다.

- 객체가 소멸되면, 다시 객체를 생성하도록 코딩하면 된다.

작성법)

public class Test{

private static Test test;

1. 클래스외부에서 직접 객체화 할 수 없도록 만든다. (생성자의 접근지정자를 private으로)

private Test(){

}

2. 클래스외부에서 생성된 객체를 얻을 수 있는 method를 작성

public static Test getInstance(){

// 객체를 하나로 유지하여

if( test == null){

test=new Test();

}

//객체를 반환하다.

return test;

}

김르르
김르르

Written by 김르르

0 Followers

르르입니다

No responses yet