Discussion on the singleton mode of C ++ Design Mode

Source: Internet
Author: User

Discussion on the singleton mode of C ++ Design Mode

Singleton mode: ensures that a class has only one instance and provides a global access point to access it.

Generally, we can make a global variable to make an object accessible, but it cannot prevent customers from instantiating multiple objects. The best way is to let the class itself protect its unique instance, this class can ensure that no other instance can be created, and it can provide a method to access the instance.

Singleton encapsulates its unique instance, so that it can strictly control how the customer accesses it and when to access it. Simply put, it is controlled access to the unique instance.

Implementation principle: privatize the constructor by providing only one static method to create an object.

(1) set the constructor to private;

(2) declare a static field, initialize an instance, and return the Singleton object;

(3) return the unique instance using the static method or static attribute

Of course, when multiple threads are used for an object, multiple instances may be created, and the object creation process can be locked.

  • Singleton: defines an Instance operation.

 
 
  1. class Singleton{ 
  2. public: 
  3.     static Singleton* Instance(); 
  4. protected: 
  5.     Singleton(){} 
  6.     Singleton(const Singleton &instance){} 
  7.     Singleton& operator=(const Singleton &instance){} 
  8. private: 
  9.     static Singleton* instance; 
  10. }; 
  11. Singleton* Singleton::Instance(){ 
  12.     if(instance == 0) 
  13.         instance = new Singleton; 
  14.     return instance; 
  15. Singleton* Singleton::instance = 0; 

In fact, the most important thing in the singleton mode is to privatize the public constructor. In this way, the constructor's instantiation right is handed over to the class itself, so that Singleton can control the class instantiation. Of course, in addition to constructors, we also need to privatize the class control replication function copy constructor and value assignment operation. Because the client has no right to construct, there is no right to control the replication function.

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.