single case mode: divided into idle type and a hungry man type. The principle is simply that a class can only create one object and provide the user with it.
a hungry man type: 1. Create a Private object
2. Building a private constructed object
3. Provide a way to call this object externally
public class Singletest {
public static void Main (String [] args) {
Single S1 =single.getinstance ();
}
}
Class Single {
private static Single S = new single ();
Private single () {}
public static single getinstance () {
return s;
}
}
Lazy Type: public class Singletest {
public static void Main (String [] args) {
Single S1 =single.getinstance ();
}
}
Class single{
private static single s = null; Create a reference variable, but delay the creation of a class,
Private single () {}
public static single getinstance () {
if (s==null) {
Synchronized (Single.class) {//Lock to avoid creating multiple objects
if (s==null)
S=new single (); Delaying the establishment of objects in this place
}
}
return s;
}
}