Note: today, I don't know how to post code and image buttons on the csdn blog. I can't post it. I can only copy the code to explain the problem.
As for the singleton mode, it is one of the most basic modes in the design mode. To put it bluntly, ensure that there is only one instance at all times in the program, such as the Windows file management class, there is also the game Director category in cocos2dx, so how can we ensure that there is only one instance at all times? At this time, our static will take effect!
A friend asked me a few days ago what this line of code in cocos2dx meant. Why should I write it like this? The Code is as follows:
Ccsize visiblesize = ccdirector: shareddirector ()-> getvisiblesize ();
He asked: Why isn't it very troublesome to add ccdirector: shareddirector () to each call without the instance of an object to call a member function?
Yes, this is a little troublesome, but it is not worth mentioning compared to the benefits it brings to us. He guarantees that there is only one instance during the entire program running, no other objects are generated. One saves space and the other facilitates our management!
The implementation of Singleton mode, here I simulate a ccdirector, the Code is as follows:
Class ccdirector
{
PRIVATE:
Static ccdirector * pdirector;
........
........
Public:
Static ccdirector * shareddire ();
{
.........
Return pdirector;
}
.......
};
Obviously, such a class declaration has reached the standard of the above syntax. How can we instantiate this unique instance? There are two optional modes:
Lazy: This is a typical example of time-for-space exchange. It is used to determine whether to create an instance every time an instance is acquired. Of course, if no one has ever used it, it will not create an instance, which is to save space.
Hungry Chinese Style: it is typical to exchange space for time. That is to say, when a class is loaded, an instance is created. No matter whether you use it or not, you do not need to judge it every time you call it, saves running time
As to which of the two models is better, we have to weigh the time and space on our own. Each has its own advantages!
In cocos2dx, a single object is instantiated in a lazy way. The Code is as follows:
Ccdirector * ccdirector: shareddirector (void)
{
Static bool s_bfirstusedirector = true;
If (s_bfirstusedirector)
{
S_bfirstusedirector = false;
S_shareddirector.init ();
}
Return & s_shareddirector;
}
We can see that it defines a static bool variable to mark whether to create a singleton object. If not, it will be Init, if yes, it will be returned directly (but I don't understand why I have to add a bool. Isn't it a few more bytes? Can I directly judge whether the pointer is null !! Well, Cocos development team is far-sighted and I cannot understand it )!
As for the hungry Chinese expression, the init () function is called directly at the beginning to keep this object pointer! You don't have to judge every time.
Note:
1: Can I only use instance pointers? Cannot an instance object?
I have mentioned this in a very low-level book. If I remember it, Tan haoqiang's book said: the struct cannot contain its own struct variables. Of course, it can contain pointers to its own struct! So the same is true for classes. This low-level error is common for beginners!
2: Is Singleton only used to save resources?
The first thing to note is that in some cases, using the singleton mode can save resources, but the intention of Singleton mode is not only to save resources, if you use Singleton mode only to save resources, it may cause abuse of Singleton mode. The Singleton mode is used to ensure that there is only one instance throughout the application for specific purposes.
3: disadvantages of Singleton Mode
Expansion is difficult because the shareddirector static function cannot generate subclass instances. To expand, only the class is rewritten.
Implicit use causes the class structure to be unclear. For example, sometimes you do not know that a class A is a singleton class. When you read Class B, you may first read its header file or the content in the Class View, from here, you cannot know the relationship between A and B, because Class B uses the so-called shareddirefunction of Class A during implementation and cannot read this line, you will not know the dependencies between class B and class.
Causes program memory leakage. Many people call shareddireto generate a unique instance, but New is always encapsulated in shareddireand forget to release the memory.
4: under what circumstances cannot the singleton mode be used
The Singleton mode is easy to use, but it is also the most abuse mode in all design modes. You cannot use the singleton mode when your class is expected to be well extended.
Maybe you do not have to make sure that there is only one instance at the beginning of your program. If you only use it to save resources, use it with caution, because with the delay of time, your program may need to be extended.