C + + Programming exercise (+)-------The implementation of "singleton mode"

Source: Internet
Author: User

Original: http://blog.csdn.net/oohaha_123/article/details/25190833

Single-Case mode

Singleton mode is a kind of common software design pattern. In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.

For some classes in the system, only one instance is important, for example, there can be multiple print tasks in a system, but only one task is working; a system can have only one window manager or file system; A system can have only one timing tool or ID (ordinal) generator. You can only open one task Manager in Windows. If you do not use the mechanism to unique window objects, will pop up more than one window, if these windows display exactly the same content, it is a duplicate object, wasting memory resources, if these windows display inconsistent content, it means that in a moment the system has multiple states, and the actual discrepancy, but also to the user misunderstanding, I don't know which one is the real state. Therefore, it is sometimes important to ensure that an object in the system is unique, that only one instance of a class is available.


How do you ensure that there is only one instance of a class and that this instance is easy to access? Defining a global variable ensures that an object can be accessed at any time, but it does not prevent us from instantiating multiple objects. A better solution is to have the class itself responsible for preserving its only instance. This class guarantees that no other instance is created, and it can provide a way to access the instance. This is the pattern motive of the singleton pattern. (excerpt from Liu Wei's editor.) design mode. Beijing: Tsinghua University Press, 2011:134-135. )

The characteristics of the singleton pattern are 3:

1, the Singleton class can have only one instance.

2, the Singleton class must create its own unique instance.

3. The Singleton class must provide this instance to all other objects.

The Singleton pattern contains only one character, which is Singleton. Singleton has a private constructor that ensures that the user cannot instantiate it directly with new. In addition, the schema contains a static private member variable instance with the static Public method instance (). The Instance method is responsible for verifying and instantiating itself, and then storing it in a static member variable to ensure that only one instance is created.

The test code is as follows:

[CPP]View Plaincopy
  1. /* C + + Singleton mode */
  2. #include <iostream>
  3. Using namespace std;
  4. Class Singleton
  5. {
  6. Private
  7. Singleton () //constructor is private
  8. {
  9. cout<<"creat Singleton."  <<endl;
  10. }
  11. static singleton* m_instance;
  12. Public
  13. static singleton* getinstance ()
  14. {
  15. if (m_instance = = NULL) //Determine if the call is the first time
  16. M_instance = new Singleton ();
  17. return m_instance;
  18. }
  19. };
  20. singleton* singleton::m_instance = NULL;
  21. int main ()
  22. {
  23. singleton* P1 = Singleton::getinstance ();
  24. singleton* P2 = p1->getinstance ();
  25. singleton* p3 = singleton::getinstance ();
  26. Singleton &ref = *singleton::getinstance ();
  27. cout<<p1<<endl;
  28. cout<<p2<<endl;
  29. cout<<p3<<endl;
  30. cout<<&ref<<endl;
  31. }

Test results such as:


As you can see from the test results diagram, the main function creates 4 instances of the Singleton class, but the constructor is actually called only the first time, and 4 times the creation returns the same pointer, indicating that getinstance () uses lazy initialization, This means that the return value is created when the function is first accessed.

C + + programming exercise (+)-------The implementation of the singleton 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.