Public Class Singleton
{
// private static bool instance_flag = false; instance flag is useless, directly determine if instance is instance, can solve this problem.
Private static singleleton
PUBLIC STRING NAME;
Private singleton () // Change the Singleton function to private to prevent it from calling New to create it.
{
Name = "guqi";
Console.writeline ("You can only see me once");
}
Public Static Singleton getInstance ()
{
IF (instance == null) {
// instance_flag = true;
Instance = new singleleton ();
Return Instance;
}
Else Return Instance;
}
}
Thank you VB1980 improvements to the code.
Public Static Singleton getInstance ()
{
IF (instance == null)
{
Instance = new singleleton ();
}
Return Instance;
}
Many simple.