사용자 도구
관리
로그인
추적:
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== Singleton 패턴 ====== ===== 타입 ===== Creational Pattern ===== 문제 ===== 객체를 단 한개만 만들어야 한다. ===== 해결 ===== 클래스의 생성자, 파괴자를 private으로 선언하여 객체의 임의 생성, 파괴를 막는다. 그리고 객체를 생성하거나 얻는 메쏘드를 따로 정의한다. ===== 클래스 다이어그램 ===== {{:programming:design_pattern:singleton.png|}} ===== 예제 ===== <file cpp singleton1.cpp> class StringSingleton { public: // Some accessor functions for the class, itself std::string GetString() const {return mString;} void SetString(const std::string &newStr) {mString = newStr;} // The magic function, which allows access to the class from anywhere // To get the value of the instance of the class, call: // StringSingleton::Instance().GetString(); static StringSingleton &Instance() { // This line only runs once, thus creating the only instance in existence static StringSingleton *instance = new StringSingleton; // dereferencing the variable here, saves the caller from having to use // the arrow operator, and removes temptation to try and delete the // returned instance. return *instance; // always returns the same instance } private: // We need to make some given functions private to finish the definition of the singleton StringSingleton(){} // default constructor available only to members or friends of this class // Note that the next two functions are not given bodies, thus any attempt // to call them implicitly will return as compiler errors. This prevents // accidental copying of the only instance of the class. StringSingleton(const StringSingleton &old); // disallow copy constructor const StringSingleton &operator=(const StringSingleton &old); //disallow assignment operator // Note that although this should be allowed, // some compilers may not implement private destructors // This prevents others from deleting our one single instance, which was otherwise created on the heap ~StringSingleton(){} private: // private data for an instance of this class std::string mString; }; </file> <file cpp singleton2.cpp> #include <iostream> using namespace std; /* Place holder for thread synchronization mutex */ class Mutex { /* placeholder for code to create, use, and free a mutex */ }; /* Place holder for thread synchronization lock */ class Lock { public: Lock(Mutex& m) : mutex(m) { /* placeholder code to acquire the mutex */ } ~Lock() { /* placeholder code to release the mutex */ } private: Mutex & mutex; }; class Singleton { public: static Singleton* GetInstance(); int a; ~Singleton() { cout << "In Destructor" << endl; } private: Singleton(int _a) : a(_a) { cout << "In Constructor" << endl; } static Mutex mutex; // Not defined, to prevent copying Singleton(const Singleton& ); Singleton& operator =(const Singleton& other); }; Mutex Singleton::mutex; Singleton* Singleton::GetInstance() { Lock lock(mutex); cout << "Get Instance" << endl; // Initialized during first access static Singleton inst(1); return &inst; } int main() { Singleton* singleton = Singleton::GetInstance(); cout << "The value of the singleton: " << singleton->a << endl; return 0; } </file> ===== 참고 ===== http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns#Singleton
문서 도구
문서 보기
이전 판
역링크
PDF로 내보내기
맨 위로
PDF Export
내용으로 건너뛰기
OBG WiKi
사이트 도구
검색
최근 바뀜
미디어 관리자
사이트맵