1. LanHanShi
A static variable
public class StaticVariable { /** * 1. New */ private StaticVariable(){} /** * 2. StaticVariable */ private final static StaticVariable singleton = new StaticVariable(); Public static static variable getInstance(){return static static variable getInstance(); }}Copy the code
Static code block
public class StaticCodeBlock { /** * 1. New */ private StaticCodeBlock(){} private static StaticCodeBlock singleton; Static {singleton = new StaticCodeBlock(); static {singleton = new StaticCodeBlock(); } /** * public static static codeblock getInstance(){return static codeblock getInstance(); }}Copy the code
2. The hungry
A static method
Public class StaticMethod {private StaticMethod(){} private static StaticMethod singletonThree; public static StaticMethod getInstance(){ if (singletonThree == null){ singletonThree = new StaticMethod(); } return singletonThree; }}Copy the code
Synchronized methods
SynchronizationMethod {private} public class SynchronizationMethod {private} public class SynchronizationMethod {private SynchronizationMethod(){} private static SynchronizationMethod instance; public static synchronized SynchronizationMethod getInstance(){ if (instance == null){ instance = new SynchronizationMethod(); } return instance; }}Copy the code
Static inner class loading
//SingletonSeven SingletonInstance is not loaded. SingletonInstance is loaded only once to ensure lazy loading and singleton mode. Public Class StaticInnerClass {private StaticInnerClass(){} private static class SingletonInstance{ private static final StaticInnerClass instance = new StaticInnerClass(); } public static StaticInnerClass getInstance(){ return SingletonInstance.instance; }}Copy the code
Sync code fast
Public class FastSynchronizationCode {public class FastSynchronizationCode {public class FastSynchronizationCode {public class FastSynchronizationCode { private FastSynchronizationCode(){} private static FastSynchronizationCode instance; private static FastSynchronizationCode getInstance(){ if (instance == null){ synchronized (FastSynchronizationCode.class){ instance = new FastSynchronizationCode(); } } return instance; }}Copy the code
Double check
Public class DCL {private DCL(){} /** * volatile updates changes to memory immediately (visibility order - preventing instruction reordering) static volatile DCL instance; private static DCL getInstance(){ if (instance == null){ synchronized (DCL.class){ if (instance == null){ instance = new DCL(); } } } return instance; }}Copy the code
3. The enumeration
Public enum Enumerate {INSTANCE; public enum Enumerate {INSTANCE; } class Test { public static void main(String[] args) { System.out.println(Enumerate.INSTANCE == Enumerate.INSTANCE); }}Copy the code
4. The application
We JDK, java.lang.Runtime is the classic singleton mode (hungry Handian) source simple to view
Notes and details for singleton mode:
- The singleton mode ensures that only one object of the class exists in the system memory, saving system resources. For some objects that need to be created and destroyed frequently, the singleton mode can improve system performance
- When instantiating a singleton class, you must remember to use the corresponding method to get the object instead of using new
- Scenarios where the singleton pattern is used: objects that need to be created and destroyed frequently, objects that take too much time or consume too much resources to create (i.e., heavyweight objects) but are frequently used, tool class objects, objects that frequently access databases or files (such as data sources, session factories, etc.)