Since it is required to generate only one instance, we must set the constructor to a private function to prohibit other instances from being created. We can define a static instance and create the instance when needed.
Here are the code for several singleton patterns:
/** * Singleton Mode * * */public class No2 {/** * singleton mode, lazy, thread-safe */public static class singleton{
Private final static SingleTon INSTANCE = new SingleTon ();
Private SingleTon () {} public static SingleTon getinstance () {return INSTANCE; }}/** * Singleton mode, a hungry man type, thread insecure */public static class singleton2{private static SingleTon2 INSTA
NCE = null;
Private SingleTon2 () {} public static SingleTon2 getinstance () {if (INSTANCE = = null) {
INSTANCE = new SingleTon2 ();
} return INSTANCE; }}/** * Singleton mode, a hungry man, thread-safe, multi-threaded environment inefficient * * public static class singleton3{private static Singl
ETon3 INSTANCE = null; Private SingleTon3 () {} public static synchronized SingleTon3 getinstance () {if (INSTANCE = =
NULL) {INSTANCE = new SingleTon3 (); } return INSTANCE; }}/** * Singleton mode, a hungry man type, another lock method */public static class singleton4{private static SingleTon4 INS
tance = null;
Private SingleTon4 () {} public static SingleTon4 getinstance () {if (INSTANCE = = null) { Synchronized (Singleton4.class) {if (INSTANCE = = null) {INSTANCE = new
SingleTon4 ();
}}} return INSTANCE; }}/** * Singleton mode, a hungry man type, variant, thread safe */public static class singleton5{private static SingleTon5 in
stance = NULL;
static{INSTANCE = new SingleTon5 ();
} private SingleTon5 () {} public static SingleTon5 getinstance () {return INSTANCE; }}/** * Singleton mode, lazy, use static inner class, thread safety "recommended" */public static class singleton6{private final Static Class Singletonholder{private static SingleTon6 INSTANCE = new SingleTon6 (); } private SingleTon6 () {} public static SingleTon6 getinstance () {return Singletonho Lder.
INSTANCE;
}}/** * Singleton mode, static inner class, using enumeration method, thread safety "recommended" */public enum singleton7{INSTANCE; public void Whatevermethod () {}}/** * Singleton mode, static inner class, using double check lock, thread safety "recommended" */public static class
singleton8{private volatile static SingleTon8 INSTANCE = null;
Private SingleTon8 () {} public static SingleTon8 getinstance () {if (INSTANCE = = null) { Synchronized (Singleton8.class) {if (INSTANCE = = null) {INSTANCE = new SingleTon
8 ();
}}} return INSTANCE; }} public static void Main (string[] args) {System.out.println (singleton.getinstance () = =Singleton.instance);
System.out.println (singleton2.getinstance () = = Singleton2.getinstance ());
System.out.println (singleton3.getinstance () = = Singleton3.getinstance ());
System.out.println (singleton4.getinstance () = = Singleton4.getinstance ());
System.out.println (singleton5.getinstance () = = Singleton5.getinstance ());
System.out.println (singleton6.getinstance () = = Singleton6.getinstance ());
System.out.println (singleton7.instance = = singleton7.instance);
System.out.println (singleton8.getinstance () = = Singleton8.getinstance ()); }
}
GitHub Source
GitHub on the code please click here