C ++ in simple mode-not just a singleton

Source: Internet
Author: User

You may be surprised to see my title. Why is it not just the singleton mode? The pattern is broken! I would like to share with you not only the Singleton mode in JAVA design mode, but also the Singleton mode in C ++ and the principle of Singleton mode. In a word, I'm not convinced!
 
For many workers who have been engaged in JAVA programming or C ++ programming for more than one year, I am familiar with the singleton mode and cannot be familiar with it any more? So easy! Simply put, it is the only way to maintain global access to an instance. In other words, whether we want to access an instance of a class in code of any other class, we can use the singleton mode or globally unique instance, like the following code:
[Java]
Class Singleton {
Private static Singleton instance;
Private Singleton (){
// Todo
}
Public static Singleton getInstance (){
If (instance = null ){
Instance = new Singleton ();
}
Return instance;
}
}
 
The above section is a simple implementation of Java Singleton mode, which deserves our attention:
(1) the construction method of Singleton class is private, and its member objects are also private. This ensures that instances of this class can only be accessed through the methods provided in Singleton mode.
(2) The member object instance is static, which is the principle and focus of the singleton mode. Why? In java, the meaning of the static keyword is that it does not belong to any class instance, but to the class itself. More importantly, static modified objects and variables will be stored in the static storage area of the memory when the class is loaded for the first time. As long as the program is running, this area will not be cleared, that is to say, the static modified instance will always be stored in the static storage area and there is only one copy. So when other classes access the instance, they all access this area, which is the principle of the singleton mode.
(3) You can use the following methods to access Singleton instances in code of other classes:
[Java]
Singleton singleton = Singleton. getInstance ();
 
(4) When it comes to a single question, it is best to add a synchronization keyword to the getInstance method in the case of multiple threads to avoid unexpected situations.
Now, we have discussed the singleton mode in java. What is the singleton mode in C ++? In fact, it is similar, but C ++ has a global variable, so it is slightly different. Let's take ProcessState. cpp in the Android system as an example to explain:
In Static. h of frameworks/base/include/private/binder, a global variable is declared.
[Cpp]
// For ProcessState. cpp
Extern sp <ProcessState> gProcess;
 
In the ProcessState. cpp of frameworks/base/libs/binder, the singleton mode is provided for this global variable:
[Cpp]
# Include <private/binder/Static. h>
...................................
Sp <ProcessState> ProcessState: self ()
{
If (gProcess! = NULL) return gProcess;

AutoMutex _ l (gProcessMutex );
If (gProcess = NULL) gProcess = new ProcessState;
Return gProcess;
}
 
ProcessState is unique for every process. Therefore, the Android system designs a singleton mode for ProcessState to access it, just like the following code.
[Cpp]
ProcessState: self ()
 
ProcessState constructor is private in ProcessState. h, so we cannot directly call constructor in other classes to instantiate it.
[Cpp]
Private:
Friend class IPCThreadState;

ProcessState ();
~ ProcessState ();

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.