This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge
1. What is the singleton pattern
Definition of the singleton pattern in Java: a class has one and only one instance, and it is instantiated to provide the entire system. As the name implies, a class has only one and only one object in a Java program.
2. How to write the singleton pattern
Singleton mode can be divided into hungry type and lazy type according to loading time. Hunny is loaded during class loading, regardless of whether an object of that class is used, whereas slacker is loaded when an object of that class is needed.
3. JAVA implementation code of singleton pattern
3.1 the hungry
There are three types of singletons of hungrier style.Copy the code
1. Direct instantiation
Public class Singleton1 {/ * * * 1, constructor privatisation * 2, create, and use a static variable to hold the * 3 * 4, to provide instance, stressed that this is a singleton, */ private Singleton1() {} public static final Singleton1 INSTANCE = new Singleton1(); }Copy the code
2. Enumeration method
*/ INSTANCE private Singleton2 INSTANCE; */ INSTANCE private Singleton2 INSTANCE; SomeThing() { instance = new Singleton2 (); } public Resource getInstance() { return instance; }}Copy the code
3. The static block
Public static final Singleton3 {public static final Singleton3 INSTANCE; private String info; static { try { INSTANCE = new Singleton3("123"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } private Singleton3(String info) { this.info = info; }}Copy the code
3.2 LanHanShi
There are also two types of lazy singletons;
1. The thread is unsafe
Public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4 {public class Singleton4; private Singleton4() {} public static Singleton4 getInstance() { if (instance == null) { instance = new Singleton4(); } return instance; }}Copy the code
2. Thread safety
Implementation method 1:
Public class Singleton5 {/ * * * 1, constructor privatisation * 2, preserve the only example of this in a static variable * 3, provides a static method, obtaining the instance objects * / static Singleton5 instance; private Singleton5() {} public static Singleton5 getInstance() { if (instance == null) { synchronized (Singleton5.class) { if (instance == null) { instance = new Singleton5(); } return instance; } } return instance; }}Copy the code
Implementation method 2:
Public class Singleton6 {/ * * * 1, inner classes are loaded and initialized, to create the INSTANCE INSTANCE object * 2, static inner class does not automatically create, as the external load initialization and initialization, * 3, because it is created when the internal class is loaded and initialized. */ private Singleton6(){} public static class Inner{private static final Singleton6 INSTANCE = new Singleton6(); } public static Singleton6 getInstance() { return Inner.INSTANCE; }}Copy the code
4. Singleton mode action
Reduce unnecessary memory overhead; Improve program performance.