갑자기 오늘 프로젝트 중에 아는형이 Singleton패턴에 대해 공부하라고 하더라.
그게 뭔지는 그냥 들어만 봤지 그게 뭔데..?? 그랬더니...
지금 우리가 프로젝트 하는 소스에 Singleton패턴이 있다는것이었다.
그러면서 하는 이게 Singleton패턴이야~~ㅋㅋㅋ
그냥 간단히 소개해 볼까 한다. (허접이겠지만.)
Singleton패턴 단 하나의 인스턴스만 생성하는것!!
간단하지 않은가?? ㅋㅋㅋ
자세히 말하자면 전역변수와 같이 어디서든지 유일한 인스턴스에 접근하여 사용한다는 패턴인거다.
Example
public class Singleton
{
private static Singleton singleton;
private Singleton()
{
} //end of constructor
public static Singleton getInstance()
{
if(Singleton == null)
singleton = new Singleton();
return singleton;
} //end of method
} // end of class
{
private static Singleton singleton;
private Singleton()
{
} //end of constructor
public static Singleton getInstance()
{
if(Singleton == null)
singleton = new Singleton();
return singleton;
} //end of method
} // end of class
참고 사이트:
http://www.dofactory.com/
TAG singleton