1. Normal Singleton
Copy codeThe Code is as follows: # include <iostream>
Using namespace std;
Class Singleton
{
Public:
Static Singleton * getInstance ();
Private:
Static Singleton * instance;
Singleton ()
{
Cout <"constructor \ n ";
// Do something
};
~ Singleton ()
{
Cout <"destructor \ n ";
// Do something
}
};
Singleton * Singleton: instance = NULL;
Singleton * Singleton: getInstance ()
{
If (instance = NULL)
Instance = new Singleton ();
Return instance;
}
Int main ()
{
Cout <"begin main \ n ";
Singleton * instance = Singleton: getInstance ();
Cout <"end main \ n ";
}
However, in this way, the constructor will not be called automatically and the user of this class needs to manually delete the instance. This is not good.
This is a lazy method, and synchronization is required in the case of multiple threads. It can also be written as a hungry Chinese, but it is not good to write in c ++ as a hungry Chinese. If there are multiple Singleton classes that are referenced by each other, then there may be problems with hunger. In C ++, the initialization sequence of the static members of these Singleton classes is uncertain.
Ii. Singleton of 2B
Copy codeThe Code is as follows: # include <iostream>
Using namespace std;
Class Singleton
{
Public:
Static Singleton * getInstance ();
Private:
Static Singleton * instance;
Singleton ()
{
Cout <"constructor \ n ";
// Do something
};
~ Singleton ()
{
Cout <"destructor \ n ";
// Do something
}
Class Garbo
{
Public:
~ Garbo ()
{
Cout <"Garbo destructor \ n ";
If (Singleton: instance! = NULL)
Delete Singleton: instance;
}
};
Static Garbo garbo;
};
Singleton * Singleton: instance = NULL;
Singleton: Garbo Singleton: garbo;
Singleton * Singleton: getInstance ()
{
If (instance = NULL)
Instance = new Singleton ();
Return instance;
}
Int main ()
{
Cout <"begin main \ n ";
Singleton * instance = Singleton: getInstance ();
Cout <"end main \ n ";
}
Uses an internal class Garbo. Since garbo is automatically released after the main is completed, garbo will call the destructor of the instance.
Iii. Literature Singleton
Copy codeThe Code is as follows: # include <iostream>
Using namespace std;
Class Singleton
{
Public:
Static Singleton * getInstance ();
Private:
Static Singleton instance;
Singleton ()
{
Cout <"constructor \ n ";
// Do something
};
~ Singleton ()
{
Cout <"destructor \ n ";
// Do something
}
};
Singleton: instance;
Singleton * Singleton: getInstance ()
{
Return & instance;
}
Int main ()
{
Cout <"begin main \ n ";
Singleton * instance = Singleton: getInstance ();
Cout <"end main \ n ";
}
The instance has been constructed before the start of main and will be automatically released after the end of main.
But in this case, it can only be a hungry singleton. If you need to apply for a large amount of resources in the constructor, these resources will remain in the entire running stage. No matter when these resources are required or not required.
Iv. Improved literature and art Singleton
Copy codeThe Code is as follows: # include <iostream>
Using namespace std;
Class Singleton
{
Public:
Static Singleton * getInstance ();
Private:
Singleton ()
{
Cout <"constructor \ n ";
// Do something
};
~ Singleton ()
{
Cout <"destructor \ n ";
// Do something
}
};
Singleton * Singleton: getInstance ()
{
Static Singleton instance;
Return & instance;
}
Int main ()
{
Cout <"begin main \ n ";
Singleton * instance = Singleton: getInstance ();
Cout <"end main \ n ";
}
In this way, we can construct singleton as needed, that is, lazy.