singleton

ensure a class has only one instance and provide a global point of access

applicability

  • only one instance of a class
  • instance must be extendable by subclassing

implementation

     // .h file 
  
     class Singleton {
     public:
  	static Singleton instance();
     protected: 
limit access
Singleton(); private: static Singleton* _instance; }; // .c file Singleton* Singleton::_instance = 0; Singleton* Singleton::instance() { if (_instance == 0) _instance = new Singleton(); return _instance; }

consequences

  • coordinate access to a single instance
  • reduced namespace
  • permit refinement
  • permit variable number of instances

etcetera

see GOF