Single column design mode
Design rules of singleton mode:
1. Constructor privatization
2. Create an instance inside a class
3. Provide a static method to get an instance
Although there are many implementation methods of singleton mode, all methods comply with these three rules!
Advantages of singleton mode:
1. Ensure that there is only one instance object of a class during the running of the program
2. It reduces the frequent creation and destruction of objects and reduces the pressure on the server
1. Hungry Chinese style (static attribute)
class Singleton { // Static properties private static final Singleton singleton = new Singleton(); // Privatization constructor private Singleton() { } // Provide external instance obtaining methods public static Singleton getInstance() { return singleton; } }
2. Hungry Chinese style (static code block)
class Singleton { // Static properties private static final Singleton singleton; // Static code block static { singleton = new Singleton(); } // Privatization constructor private Singleton() { } // Provide external instance obtaining methods public static Singleton getInstance() { return singleton; } }
These two kinds of hungry Chinese style are only different in writing, and their advantages and disadvantages are the same
advantage:
The writing method is simple. The instance has been created when the class is loaded, and there will be no thread safety problem
Disadvantages:
The resource consumption during class loading is increased, and the class loading speed will be reduced No matter whether the current class is used or not, an instance will be created, which increases the consumption of memory.
3. Lazy (single thread)
class Singleton { // Static properties private static Singleton singleton = null; // Privatization constructor private Singleton() { } // Provide external instance obtaining methods public static Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; } }
advantage:
The writing method is simple. Class instances are created only when they are used. Compared with the hungry Chinese style, it avoids the consumption of memory.
Disadvantages:
It is only reliable under single thread, and there will be thread safety problems under multi thread. Because the instance is created only when it is used, it is slower to get the object instance for the first time than the hungry man style.
4. Lazy (synchronous method)
class Singleton { // Static properties private static Singleton singleton = null; // Privatization constructor private Singleton() { } // Provide external instance obtaining methods public static synchronized Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; } }
advantage:
The writing method is simple to ensure thread safety under multithreading
Disadvantages:
synchronized is a heavyweight lock. It will be locked every time an object is obtained, which reduces the code efficiency.
5. Lazy (double search)
class Singleton { // Static attribute, decorated with volatile private static volatile Singleton singleton = null; // Privatization constructor private Singleton() { } // Provide external instance obtaining methods public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } }
advantage:
It increases the code running efficiency. When the object pair is created, it does not need to lock every time, but only when it is created for the first time.
Disadvantages:
The single case mode of double retrieval has no obvious shortcomings, but compared with the previous method, the writing method of this method is more complex.
6. Static internal class writing
class Singleton { // Privatization constructor private Singleton() { } // Static inner class private static class SingletonInstance{ private static final Singleton INSTANCE = new Singleton(); } // Provide external instance obtaining methods public static Singleton getInstance() { return SingletonInstance.INSTANCE; } }
Static inner classes will not be loaded when the Singleton class is loaded. (there are doubts here for reference) Static internal class loading order ), the static internal classes are loaded only when they are used. At the same time, the jvm naturally ensures that class loading is thread safe.
advantage:
Thread safe. Objects are loaded only when they are used,
Disadvantages:
The writing method is not as simple as other methods
7. Enumeration
enum Singleton{ INSTANCE; }
Enumeration naturally ensures that there is only one instance. The construction method of enumeration is internally optimized and does not need us to deal with it.
advantage:
It is simple to write, thread safe, and can avoid creating objects during deserialization.