Design Patterns
Материал из Wiki.crossplatform.ru
(Различия между версиями)
ViGOur (Обсуждение | вклад) (Новая страница: «= Порождающие паттерны проектирования = == Абстрактная фабрика (Abstract Factory, Factory), др. название…») |
ViGOur (Обсуждение | вклад) (→Одиночка (Singleton)) |
||
Строка 2: | Строка 2: | ||
== Абстрактная фабрика (Abstract Factory, Factory), др. название Инструментарий (Kit) == | == Абстрактная фабрика (Abstract Factory, Factory), др. название Инструментарий (Kit) == | ||
== Одиночка (Singleton) == | == Одиночка (Singleton) == | ||
+ | === Статический === | ||
+ | <source lang="cpp">class CSingleton | ||
+ | { | ||
+ | private: | ||
+ | static CSingleton m_singleton; | ||
+ | |||
+ | private: | ||
+ | CSingleton() {} | ||
+ | ~CSingleton() {} | ||
+ | CSingleton(const CSingleton &) {} | ||
+ | CSingleton & operator=(const CSingleton &) { return *this; } | ||
+ | |||
+ | public: | ||
+ | static CSingleton *instance() { return &m_singleton; } | ||
+ | }; | ||
+ | |||
+ | CSingleton CSingleton::m_singleton; | ||
+ | |||
+ | int main(int , char **) | ||
+ | { | ||
+ | CSingleton *p = CSingleton::instance(); | ||
+ | // ... | ||
+ | return 0; | ||
+ | }</source> | ||
+ | |||
+ | === Динамический === | ||
+ | <source lang="cpp">class CSingleton | ||
+ | { | ||
+ | private: | ||
+ | static CSingleton *m_pSingleton; | ||
+ | static int m_nCounter; | ||
+ | |||
+ | private: | ||
+ | CSingleton() {} | ||
+ | ~CSingleton() {} | ||
+ | CSingleton(const CSingleton &) {} | ||
+ | CSingleton & operator=(const CSingleton &) { return *this; } | ||
+ | |||
+ | public: | ||
+ | static CSingleton *instance() | ||
+ | { | ||
+ | if( m_nCounter == 0 ) | ||
+ | { | ||
+ | m_pSingleton = new CSingleton(); | ||
+ | } | ||
+ | m_nCounter++; | ||
+ | return m_pSingleton; | ||
+ | } | ||
+ | static CSingleton *freeInstance() | ||
+ | { | ||
+ | if( m_nCounter > 0 ) | ||
+ | { | ||
+ | m_nCounter--; | ||
+ | if( m_nCounter == 0 ) | ||
+ | { | ||
+ | delete m_pSingleton; | ||
+ | m_pSingleton = 0; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | CSingleton *CSingleton::m_pSingleton = 0; | ||
+ | int CSingleton::m_nCounter = 0; | ||
+ | |||
+ | int main(int , char **) | ||
+ | { | ||
+ | CSingleton *p = CSingleton::instance(); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </source> | ||
+ | |||
== Прототип (Prototype) == | == Прототип (Prototype) == | ||
== Создатель экземпляров класса (Creator) == | == Создатель экземпляров класса (Creator) == |
Версия 07:18, 16 апреля 2013
Порождающие паттерны проектирования
Абстрактная фабрика (Abstract Factory, Factory), др. название Инструментарий (Kit)
Одиночка (Singleton)
Статический
class CSingleton { private: static CSingleton m_singleton; private: CSingleton() {} ~CSingleton() {} CSingleton(const CSingleton &) {} CSingleton & operator=(const CSingleton &) { return *this; } public: static CSingleton *instance() { return &m_singleton; } }; CSingleton CSingleton::m_singleton; int main(int , char **) { CSingleton *p = CSingleton::instance(); // ... return 0; }
Динамический
class CSingleton { private: static CSingleton *m_pSingleton; static int m_nCounter; private: CSingleton() {} ~CSingleton() {} CSingleton(const CSingleton &) {} CSingleton & operator=(const CSingleton &) { return *this; } public: static CSingleton *instance() { if( m_nCounter == 0 ) { m_pSingleton = new CSingleton(); } m_nCounter++; return m_pSingleton; } static CSingleton *freeInstance() { if( m_nCounter > 0 ) { m_nCounter--; if( m_nCounter == 0 ) { delete m_pSingleton; m_pSingleton = 0; } } } }; CSingleton *CSingleton::m_pSingleton = 0; int CSingleton::m_nCounter = 0; int main(int , char **) { CSingleton *p = CSingleton::instance(); return 0; }