Singleton Mode
As the name implies, it isProgramThere is only one instance of this class, which is equivalent to the global variable in C ++. This instance is initialized only once. Other Objects obtain this object through related methods:
The Singleton mode can be used in the following ways:
1. the constructor of a class is a private function, that is, other classes are not allowed to instantiate the class.
2. You have a static object of your own type as a globally unique object.
3. You have a method to obtain the static state of a unique object. object initialization can be put in this method.
Example:
Public Class {
Private Static a SA; // It must be static
Public static a instance () // It must be static; otherwise, it cannot be called directly through the class.
{
If (SA = NULL) {SA = new ();}
Return SA;
}
Private A () {// constructor of Privatization
//.....
}
Public void otherfunction (){
//.........
}
}