어제 ‘Programming game AI by example’을 읽던 중 Singleton이란 개념을 처음(아마 한 8년 전 쯤에는 알았을지도.) 접했다.
If singletons are a new concept to you, and you decide to search the internet for further information, you will discover they fuel many a good argument about the design of object-oriented software. Oh yes, programmers love to argue this stuff, nothing stokes a dispute better than the discussion of global variables or object that masquerade as globals, such as singltons. My own stance on the matter is to use them wherever I think they provide a convenience and, in my opinion, do not compromise the design. I recommend you read the arguments for an against though, and come to your own conclusions. A good staring place is here: http://c2.com/cgi/wiki?SingletonPattern
– p59, Programming game AI by design
‘Head first design patterns’에서는 Singleton을 이렇게 정의하고 있다.
The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
– p177 Head first design patterns
다시 ‘Programming game AI by example’로 돌아오면
There are many ways of implementing a singleton. I prefer to use a static method, Instance. that returns a pointer to a static instance of the class. Here’s an example:
– p59, Programming Game AI by example.
static 키워드를 사용하는 간단한 방법이다. 단점이라면 한번 MyClass::Instance()로 인스턴스를 만들면 그걸 메모리로 부터 제거할 수 있는 방법이 없어 보인다.