?? singleton.c
字號:
/**/#ifdef Implementation1/**/class Singleton {public: static Singleton* Instance();protected: Singleton();private: static Singleton* _instance;};/**/Singleton* Singleton::_instance = 0;Singleton* Singleton::Instance () { if (_instance == 0) { _instance = new Singleton; } return _instance;}/**/#endif/**/#ifdef Implementation2#include "List.H"#include "stdlib.h"class NameSingletonPair;/**/class Singleton {public: static void Register(char* name, Singleton*); static Singleton* Instance();protected: static Singleton* Lookup(const char* name);private: static Singleton* _instance; static List<NameSingletonPair>* _registry;};/**/Singleton* Singleton::Instance () { if (_instance == 0) { const char* singletonName = getenv("SINGLETON"); // user or environment supplies this at startup _instance = Lookup(singletonName); // Lookup returns 0 if there's no such singleton } return _instance;}/**/class MySingleton : public Singleton {public: MySingleton();};/**/MySingleton::MySingleton() { // ... Singleton::Register("MySingleton", this);}/**/static MySingleton theSingleton;/**/#endif/**/#ifdef Singleton/**/class MazeFactory {public: static MazeFactory* Instance(); // existing interface goes hereprotected: MazeFactory();private: static MazeFactory* _instance;};/**/MazeFactory* MazeFactory::_instance = 0;MazeFactory* MazeFactory::Instance () { if (_instance == 0) { _instance = new MazeFactory; } return _instance;}/**/#else//MazeFactory* MazeFactory::_instance = 0;#include "C++/MazeFactories.H"#include "stdlib.h"#include "strings.h"/**/MazeFactory* MazeFactory::Instance () { if (_instance == 0) { const char* mazeStyle = getenv("MAZESTYLE");/**/ if (strcmp(mazeStyle, "bombed") == 0) { _instance = new BombedMazeFactory;/**/ } else if (strcmp(mazeStyle, "enchanted") == 0) { _instance = new EnchantedMazeFactory;/**/ // ... other possible subclasses/**/ } else { // default _instance = new MazeFactory; } } return _instance;}/**/#endif/**/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -