Design Mode C ++ description ---- 01. Singleton Mode

Source: Internet
Author: User

1. Concepts

Singleton mode: it is designed to ensure that a class has only one instance and provides a global access point for it. This instance is shared by all program modules.

[Cpp]
Class CSingleton
{
// Public static method to obtain the instance
Public:
Static CSingleton * GetInstance ()
{
If (m_pInstance = NULL) // determines whether the first call is successful.
M_pInstance = new CSingleton ();
 
Return m_pInstance;
}
 
// Private constructor to prevent instantiation
Private:
CSingleton (){};
 
 
// Private Static pointer variable pointing to the unique instance of the class
Private:
Static CSingleton * m_pInstance; // declare a static member
};
 
CSingleton * CSingleton: m_pInstance = NULL; // defines and initializes static data members.
 
Int main ()
{
CSingleton * ps1 = CSingleton: GetInstance ();
CSingleton * ps2 = CSingleton: GetInstance ();
CSingleton * ps3 = ps1-> GetInstance ();
CSingleton & ps4 = * CSingleton: GetInstance ();
 
If (ps1 = ps2)
{
Cout <"ps1 = ps2" <endl;
}
 
If (ps1 = ps3)
{
Cout <"ps1 = ps3" <endl;
}
 
If (& ps4 = ps1)
{
Cout <"ps1 = ps4" <endl;
}
 
Return 0;
}
The Singleton mode manages its unique instances through the class itself. The unique instance is a common object of the class, but when designing this class, allow it to create only one instance and provide global access to the instance.

Only the GetInstance () member function is used to access a unique instance. If you do not use this function, any attempt to create an instance will fail because the class constructor is private.

Note that CSingleton * CSingleton: m_pInstance = NULL must be added. Otherwise, compilation will fail because this is the variable definition.

Ii. Singleton has the following characteristics: www.2cto.com

It has a static pointer m_pInstance pointing to a unique instance and is private;

It has a public function that obtains the unique instance and creates the instance as needed;

Its constructor is private, so that you cannot create instances of this class elsewhere.

Iii. Existing Problems

1. When will the space pointed to by m_pInstance be released?

If there are necessary operations in the class destructor, such as closing the file and releasing external resources, the above Code cannot meet this requirement. We need a method to delete the instance normally.


Unreasonable solution:

Call GetInstance () at the end of the program and delete the returned pointer. This can implement functions, but it is not only ugly, but also prone to errors. Because such additional code is easy to forget, and it is difficult to ensure that after the delete operation, no code can call the GetInstance function. That is to say, the release operation is managed by the user rather than by the class itself. This violates the single responsibility principle of the class, which is unreasonable.

2. When will the destructor of this instance be executed?

Why is there no destructor in the above class? In fact, even if you add the destructor, this destructor will not be executed. Because your instance is new, only the delete operation will call the destructor. But where can I call the delete operation !? This is back to the above problem.

A proper method:


[Cpp]
Class CSingleton
{
Public:
Static CSingleton * GetInstance ()
{
Static CSingleton instance; // static local variable

Return & instance;
}
 
Private:
 
CSingleton () {}; // Constructor
 
};
The instance of a local static object is constructed when GetInstance () is called for the first time. It remains active until the application ends. It is different from the dynamically assigned object, static objects are automatically destroyed when the application ends, so you do not have to manually destroy the instance.
Of course, you can add the destructor to process the operations you want.

The main points of this operation are as follows:

1. The static variables have only one copy in the memory, thus ensuring the requirements of a single instance in singleton mode.

2. Static variables will be automatically destroyed when the program is terminated, thus ensuring normal space release.

 

Author lwbeyond

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.