Singleton mode: The simplest design mode
Role: is to ensure that in the entire application lifecycle, at any one time, instances of the Singleton class exist only one.
Divided into two a hungry man modes and lazy mode
A hungry man mode: When the class loads slowly but gets the object fast
public class Singleton {
The role of privatization of constructors is not to allow the creation of Singleton instances in new ways in other classes
Private Singleton () {
}
This guarantees that there can be only one instance in the global
private static Singleton Singleton = new Singleton ();
This is a package.
public static Singleton Getsingleton () {
return singleton;
}
}
Lazy mode: When the class loads faster, but gets the object slowly
public class Singleton {
The role of privatization of constructors is not to allow the creation of Singleton instances in new ways in other classes
Private Singleton () {
}
private static Singleton Singleton;
This is the encapsulation. This guarantees that there can be only one instance in the global
public static Singleton Getsingleton () {
if (singleton==null) {
Singleton = new Singleton ();
}
return singleton;
}
}
Design mode-simplest design pattern singleton mode