Created
May 17, 2016 11:44
-
-
Save AhnMo/d81c50c8456e9df2fc510b754a5ecdd9 to your computer and use it in GitHub Desktop.
16 05 17 Design Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTone 1 | |
// singleton on bss | |
#include <iostream> | |
using namespace std; | |
class Cursor { | |
private: | |
// 규칙 1. private constructor | |
Cursor() { } | |
// 규칙 2. 복사와 대입 금지 | |
Cursor(const Cursor&) = delete; | |
void operator=(const Cursor&) = delete; | |
public: | |
// 규칙3. 오직 하나의 객체만 만들어 리턴하는 스태틱 함수 | |
static Cursor& getIsntatnce() { | |
static Cursor instance; | |
return instance; | |
} | |
}; | |
int main(int argc, char **argv) { | |
Cursor& c1 = Cursor:: getIsntatnce(); | |
Cursor& c2 = Cursor:: getIsntatnce(); | |
cout << &c1 << endl; | |
cout << &c2 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTon 2 | |
// singleton on heap | |
#include <iostream> | |
using namespace std; | |
class Cursor { | |
private: | |
Cursor() { } | |
Cursor(const Cursor&) = delete; | |
void operator=(const Cursor&) = delete; | |
static Cursor* sInstance; | |
public: | |
static Cursor& getIsntatnce() { | |
// can be occur RCE | |
if (sInstance == nullptr) | |
sInstance = new Cursor; | |
return *sInstance; | |
} | |
}; | |
Cursor* Cursor::sInstance = nullptr; | |
int main(int argc, char **argv) { | |
Cursor& c1 = Cursor:: getIsntatnce(); | |
Cursor& c2 = Cursor:: getIsntatnce(); | |
cout << &c1 << endl; | |
cout << &c2 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTone 3 | |
// singleton on heap with mutex | |
#include <iostream> | |
using namespace std; | |
class Mutex { | |
public: | |
void Lock() { | |
cout << "Mutex Lock" << endl; | |
} | |
void UnLock() { | |
cout << "Unmutex Lock" << endl; | |
} | |
}; | |
class Cursor { | |
private: | |
Cursor() { } | |
Cursor(const Cursor&) = delete; | |
void operator=(const Cursor&) = delete; | |
static Cursor* sInstance; | |
static Mutex sLock; | |
public: | |
static Cursor& getIsntatnce() { | |
sLock.Lock(); | |
if (sInstance == nullptr) | |
sInstance = new Cursor; | |
sLock.UnLock(); | |
return *sInstance; | |
} | |
}; | |
Cursor* Cursor::sInstance = nullptr; | |
Mutex Cursor::sLock; | |
int main(int argc, char **argv) { | |
Cursor& c1 = Cursor:: getIsntatnce(); | |
Cursor& c2 = Cursor:: getIsntatnce(); | |
cout << &c1 << endl; | |
cout << &c2 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTon 4 | |
// singleton on heap with mutex that resolved exception | |
#include <iostream> | |
using namespace std; | |
class Mutex { | |
public: | |
void Lock() { | |
cout << "Mutex Lock" << endl; | |
} | |
void UnLock() { | |
cout << "Unmutex Lock" << endl; | |
} | |
// RARR: Resource Acquire is R | |
class Autolock { | |
Mutex& mutex; | |
public: | |
inline Autolock(Mutex& m): mutex(m) { mutex.Lock(); }; | |
inline ~Autolock() { mutex.UnLock(); }; | |
}; | |
}; | |
class Cursor { | |
private: | |
Cursor() { } | |
Cursor(const Cursor&) = delete; | |
void operator=(const Cursor&) = delete; | |
static Cursor* sInstance; | |
static Mutex sLock; | |
public: | |
static Cursor& getIsntatnce() { | |
Mutex::Autolock sl(sLock); | |
if (sInstance == nullptr) | |
sInstance = new Cursor; | |
return *sInstance; | |
} | |
}; | |
Cursor* Cursor::sInstance = nullptr; | |
Mutex Cursor::sLock; | |
int main(int argc, char **argv) { | |
Cursor& c1 = Cursor:: getIsntatnce(); | |
Cursor& c2 = Cursor:: getIsntatnce(); | |
cout << &c1 << endl; | |
cout << &c2 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTon 5 | |
// singleton with inhertance on heap with mutex that resovled excpetion | |
#include <iostream> | |
using namespace std; | |
class Mutex { | |
public: | |
void Lock() { | |
cout << "Mutex Lock" << endl; | |
} | |
void UnLock() { | |
cout << "Unmutex Lock" << endl; | |
} | |
class Autolock { | |
Mutex& mutex; | |
public: | |
inline Autolock(Mutex& m): mutex(m) { mutex.Lock(); }; | |
inline ~Autolock() { mutex.UnLock(); }; | |
}; | |
}; | |
template<typename TYPE> | |
class Singleton { | |
protected: | |
Singleton() { } | |
private: | |
Singleton(const Singleton&) = delete; | |
void operator=(const Singleton&) = delete; | |
static TYPE* sInstance; | |
static Mutex sLock; | |
public: | |
static TYPE& getIsntatnce() { | |
Mutex::Autolock sl(sLock); | |
if (sInstance == nullptr) | |
sInstance = new TYPE; | |
return *sInstance; | |
} | |
}; | |
template<typename TYPE> | |
TYPE* Singleton<TYPE>::sInstance = nullptr; | |
template<typename TYPE> | |
Mutex Singleton<TYPE>::sLock; | |
class Mouse: public Singleton<Mouse> { }; | |
class Cat: public Singleton<Cat> { }; | |
int main(int argc, char **argv) { | |
Mouse& m1 = Mouse::getIsntatnce(); | |
Cat& c1 = Cat::getIsntatnce(); | |
cout << &m1 << endl; | |
cout << &c1 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTon 6 | |
// singleton with inhertance on bss | |
#include <iostream> | |
using namespace std; | |
template<typename TYPE> | |
class Singleton { | |
protected: | |
Singleton() { } | |
private: | |
Singleton(const Singleton&) = delete; | |
void operator=(const Singleton&) = delete; | |
public: | |
static TYPE& getIsntatnce() { | |
static TYPE sInstance; | |
return sInstance; | |
} | |
}; | |
class Mouse: public Singleton<Mouse> { }; | |
class Cat: public Singleton<Cat> { }; | |
int main(int argc, char **argv) { | |
Mouse& m1 = Mouse::getIsntatnce(); | |
Cat& c1 = Cat::getIsntatnce(); | |
cout << &m1 << endl; | |
cout << &c1 << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SingleTon 7 | |
// singleton with macro | |
#include <iostream> | |
using namespace std; | |
#define MAKE_SINGLETON(classname) \ | |
private: \ | |
classname() { } \ | |
classname(const classname&) = delete; \ | |
void operator=(const classname&) = delete; \ | |
public: \ | |
static classname& getIsntatnce() { \ | |
static classname instance; \ | |
return instance; \ | |
} | |
class Cursor { | |
MAKE_SINGLETON(Cursor); | |
}; | |
int main(int argc, char **argv) { | |
Cursor& c1 = Cursor::getIsntatnce(); | |
Cursor& c2 = Cursor::getIsntatnce(); | |
cout << &c1 << endl; | |
cout << &c2 << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment