생성패턴-Singleton

2008. 8. 23. 19:22 from 셈말짓기/GoF

-----------------------------------------------------------------------------
[Singleton]
의도:
클래스에서 만들 수 있는 인스턴스가 오직 하나일 경우에 이에 대한 접근은 어디에서든지
하나로만 통일하여 제공한다.
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A creational part of GoF's Design Patterns
//
// - Singleton
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>

#include <iostream>
#include <memory>


/////////////////////////////////////////////////////////////////////////////
//
// Singleton
//
//
/////////////////////////////////////////////////////////////////////////////
class Singleton
{
protected:
 Singleton () {}
public:
 virtual ~Singleton() {}

public:
 static Singleton& Instance(void)
 {
  if (Instance_.get() == 0)
  {
   Instance_.reset( new Singleton() );
  }

  return *Instance_;
 }

private:
 static std::auto_ptr<Singleton> Instance_;

public:
 void Operation (void)
 {
  std::cout << "Singleton::Operation()" << std::endl;
 }
};

std::auto_ptr<Singleton> Singleton::Instance_;


/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
 Singleton::Instance().Operation();

 return 0;
}
-----------------------------------------------------------------------------
Singleton::Operation()

Posted by 셈말짓기 :