Design Mode-single-piece Mode

Source: Internet
Author: User

Singleton mode is also called Singleton mode and Singleton mode. The Singleton mode ensures that a class has only one instance and provides a global access point for it. This instance is shared by all program modules. Such functional modules are needed in many places, such as system log output.
Singleton mode has many implementation methods. In C ++, you can even use a global variable to do this, but such code is not elegant. The design pattern book provides a very good implementation that defines a singleton class and uses the Private Static pointer variable of the class to point to the unique instance of the class, obtain the instance using a public static method.

Method 1: Pay attention to the stack overflow problem when the class is too large.

// Singleton. hclass csingleton {PRIVATE: csingleton () {} public: static csingleton * getinstance () {static csingleton singles; // defines the static object return & singles ;}} here ;}};

Method 2: When the Destructor is not defined or defined as public.

// Singleton.hclass CSingleton{ private:     CSingleton () {}public:        static CSingleton *GetInstance();
PRIVATE: static csingleton * m_pinstance; // defines the static object class cgarbo {public :~ Cgarbo () {If (csingleton: m_pinstance) {Delete csingleton: m_pinstance; csingleton: m_pinstance = NULL ;}}; static cgarbo M _ garbosingleton ;}; // Singleton. cppcsingleton * csingleton: m_pinstance = NULL; csingleton * csingleton: getinstance () {If (null = m_pinstance) {m_pinstance = new csingleton ();} return m_pinstance ;}

Method 3: When the Destructor is defined as private or protected, use macros to make the program look concise and clear.

// Singleton. h // Its only job is to delete the cgarbo # theclass instance in the destructor. // define a static member variable. At the end of the program, the system will call its destructor. # define declare_garbo (theclass) \ class cgarbo # theclass \ {\ public :\~ Cgarbo # theclass () \{\ if (theclass: m_pinstance) \{\ Delete theclass: m_pinstance; \ theclass: m_pinstance = NULL ;\}\}\}; \ static cgarbo # theclass M _ # theclass; \ friend class cgarbo # theclass; Class csingleton {declare_garbo (csingleton) PRIVATE: csingleton (){}~ Csingleton () {} public: static csingleton * getinstance (); Private: static csingleton * m_pinstance; // static object defined here}; // Singleton. cppcsingleton * csingleton: m_pinstance = NULL; csingleton * csingleton: getinstance () {If (null = m_pinstance) {m_pinstance = new csingleton ();} return m_pinstance ;}

Both method 2 and method 3 Use cgarbo as the private embedded class of csingleton to prevent this class from being abused elsewhere. At the end of the program running, the system calls the cgarbo destructor, a static member of csingleton. This destructor deletes the unique instance of a single instance. Releasing a singleton object in this way has the following features:

A. Define a proprietary nested class within the singleton class.

B. Define Private Static members dedicated for release in the singleton class.

C. Use the program to analyze the global variables at the end and select the final release time.

D. The Singleton Code does not require any operations, and you do not need to care about the release of objects.

int main() {       CSingleton *p1 = CSingleton::GetInstance();       CSingleton *p2 = CSingleton::GetInstance();      if (p1 == p2)       {           printf("Two objects is the same instance");      }       return 0; } 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.