2. 자동적인 단일체 유틸리티
단일체 패턴singleton pattern
의도
해당 클래스의 인스턴스가 오직 하나 뿐임을 보장하고, 이에 대한 전역적인 접근점을 제공하기 위함.
구조
Singleton
static GetInstance()
SingletonOperation()
GetSingletonData()
static uniqueInstance
singletonData
return uniqueInstance
public
private
3. 자동적인 단일체 유틸리티
단일체 템플릿
단일체 클래스는 굉장히 자주 쓰인다. 그런데 이걸 매번 똑같은 형식으로 만들기 귀찮으니, 그냥
템플릿으로 만들어 버리자.
template<typename T>
class Singleton
{
public:
static T* GetInstance()
{
if (m_pInstance == nullptr)
m_pInstance = new T;
return m_pInstance;
}
void ReleaseInstance()
{
if (m_pInstance != nullptr)
{
delete m_pInstance;
m_pInstance = nullptr;
}
}
private:
static T* m_pInstance;
};
template<typename T>
T* Singleton <T>::m_pInstance = nullptr;
4. 자동적인 단일체 유틸리티
단일체 템플릿
실제 사용 예
class TestClass : public Singleton<TestClass>
{
public:
void print()
{
printf(“TEST~~~”);
}
private:
TestClass();
~TestClass();
};
int main()
{
TestClass::GetInstance()->print();
}
5. 자동적인 단일체 유틸리티
끝
‘ GPG를 공부하자’ 페이스북 페이지
https://www.facebook.com/gpgstudygogo