C + + single case mode

Source: Internet
Author: User

1. The concept and function of C + + singleton model

The singleton mode, also known as the single-piece mode, the list mode, is probably the most widely used design pattern. The intent is to ensure that a class has only one instance and provides a global access point to access it , which is shared by all program modules.

2, the realization principle of the single case mode

(1)private static pointer variableImplementation: Use the private static pointer variable of the class to point to a unique instance of the class and obtain the instance using a public static method. (2)Static Local VariablesImplementation: Defines a static local variable in the public static method that points to the class, and returns the static local variable. 3, the implementation code of the singleton mode(1)
private static pointer variable implementation
Classsingleton{Private: Singleton ()//The constructor is private { } StaticSingleton *pinstance; Public: StaticSingleton *getinstance () {if(pinstance = = NULL)//determines whether the first call isPinstance =NewSingleton (); returnpinstance; }};

(2)

Static local Variables
Class singleton{private: Singleton () // constructor is private { }public: static Singleton & getinstance () { Static Singleton instance; // local static variables return instance; }};

4. Expansion

A) Set the constructor to private, disable assignment and copy. Problem: Unable to generate objects randomly in main

b) Provide a static function that bypasses the restriction of the constructor to private. Problem: The object is not unique.

c) Set a static pointer to determine whether NULL is the first time. A simple singleton pattern is implemented at this time. But at this point in the multi-threaded environment is not unique.

D) A mutex is introduced to achieve mutually exclusive access to the critical section within the getinstance. At this point each call needs to be locked, resulting in reduced efficiency.

e) Double check mode makes it unnecessary to lock each call and improve efficiency.

5, DCLP (Double-check-locking-pattern)

classsingleton{ Public:    StaticSingleton *getinstance () {if(Pinstance_ = =NULL) {mutex_.Lock(); if(Pinstance_ = = NULL)//Switching of ThreadsPinstance_ =NewSingleton;        Mutex_.unlock (); }                returnPinstance_; }Private: Singleton () {}StaticSingleton *Pinstance_; Staticmutexlock mutex_;};

C + + single case mode

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.