Java Design Pattern Single-instance pattern (Singleton pattern)
definition of a singleton pattern:Singleton pattern restricts the instantiation of a class and ensures that's only one instance of the class Exis TS in the Java Virtual machine. The Singleton class must provide a global access point to get the instance of the class.
restricting an instance of a class ensures only one of the instances of a JVM, and must provide a global access point to obtain the singleton. Why a singleton mode is present:
- Reduce memory costs. Because the Singleton has only one instance object in memory relative to a single JVM instance, there is no duplication of creation and JVM garbage collection, which reduces space consumption for memory and facilitates JVM garbage collection processing.
- Reduce system performance overhead. When an object's production relies on more resources, such as reading a configuration or relying on other objects, the singleton preload the resource when the JVM is started, and then it can permanently reside in memory and, of course, reduce the burden on the JVM's garbage collection thread.
- Of course, there are advantages, the popular spring framework is the default support singleton mode (relative to the spring container).
How to implement a single example:There are many different ways to implement a singleton pattern, but we will consider the following points:
- Constructors must be private, not allowed to be instantiated by others
- This singleton typically has a private static variable in the class
- The singleton typically provides a static public method to obtain the singleton (global access point for the outside of the singleton)
a Hungry man type (Eager initialization)The a Hungry man singleton implementation method initializes the singleton when the class is loaded. This is the simplest way to implement a single case, but one drawback is that even if we don't use a singleton in our application, the class must be initialized when it is loaded.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Created on July 31, 2016 11:36:05 * * A hungry man single case * eagerinitialized Singleton */public class Eagerinitializedsingleton {private static final Eagerinit Ializedsingleton instance = new Eagerinitializedsingleton (); Private Eagerinitializedsingleton () {} public static Eagerinitializedsingleton getinstance () {return Instan Ce } public void DoSomething () {System.out.println ("test"); } public static void Main (string[] args) {System.out.println (eagerinitializedsingleton.getinstance ()); System.out.println (Eagerinitializedsingleton.getinstance ()); Eagerinitializedsingleton.getinstance (). dosomething (); [Email protected]139a55//[email protected]139a55//test System.out.println (eagerinitializedsing Leton.getinstance () = = Eagerinitializedsingleton.getinstance ()); True}}
When a single case does not involve excessive use of resources, a hungry man-type singleton is more suitable. However, in many scenarios, the use of Singleton is generally used to use some resources such as file system, database connection and so on, in this scenario, we generally try to make the single case must be used when it will be initialized, to avoid wasting the use of resources. Moreover, the A hungry man-type singleton does not provide an abnormal handling mechanism.
Static Block InitializationStatic block initialization is similar to Singleton and a hungry man, except that the initialization of an instance is in a static block, which provides exception handling.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Created on August 1, 2016 morning 12:30:08 */public class S Taticblocksingleton { private static Staticblocksingleton instance; public static Staticblocksingleton getinstance () { return instance; } static { try { instance = new Staticblocksingleton (); } catch (Exception e) { throw new RuntimeException ( "Exception occured in creating singleton instance"); } } public static void Main (string[] args) { System.out.println (staticblocksingleton.getinstance ()); System.out.println (Staticblocksingleton.getinstance ()); System.out.println (staticblocksingleton.getinstance () = = Staticblocksingleton.getinstance ()); [Email protected]5 //[email protected]5 //True }}
Lazy InitializationThe lazy initialization method assumes the creation of a singleton and is the entry point for the single instance.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Created on August 1, 2016 morning 12:39:40 */public class L Azyinitializedsingleton { private static Lazyinitializedsingleton instance; public static Lazyinitializedsingleton getinstance () { if (instance = = null) { instance = new Lazyinitializedsing Leton (); } return instance; } Private Lazyinitializedsingleton () { } public static void Main (string[] args) { System.out.println ( Lazyinitializedsingleton.getinstance () = = Lazyinitializedsingleton.getinstance ());//True }}
The above-mentioned single-instance implementations can work well in single-threaded environments, but may face multi-threaded security issues.
Thread Safe Singletonthread safety Singleton, simple thread safety we can use
Synchronized
implementation.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Created on August 1, 2016 morning 11:19:36 * * Thread Safe Singleton * */public class Threadsafesingleton { private static Threadsafesingleton instance; public static synchronized Threadsafesingleton getinstance () { if (instance = = null) { instance = new Threadsafesi Ngleton (); } return instance; } Private Threadsafesingleton () { } public static void Main (string[] args) { System.out.println ( Threadsafesingleton.getinstance () = = Threadsafesingleton.getinstance ()); True }}
The above line Shuo full lock granularity is relatively large, the greater the granularity of the lock, concurrency is about to fail, resulting in performance degradation, we can useDouble checked lockingrules reduce the granularity of locks.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Created on August 1, 2016 morning 11:37:25 * * Double checked locking principle */public class Doublecheckedlockingthreadsafesingleton { private static Doublecheckedlockingthreadsafesingleton instance; public static Doublecheckedlockingthreadsafesingleton getinstance () { if (instance = = null) { synchronized ( Doublecheckedlockingthreadsafesingleton.class) { if (instance = = null) { instance = new Doublecheckedlockingthreadsafesingleton (); }}} return instance; } Private Doublecheckedlockingthreadsafesingleton () { } public static void Main (string[] args) { }}
Inner Static helper class Singleton use internal classes to implement lazy loading of resources:
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * Inner static helper class Singleton * *
* Created on August 1, 2016 morning 11:54:05 */public class Innerstatichelperclasssingleton { private Innerstatichelperclasssingleton () { } public static Innerstatichelperclasssingleton getinstance () { return singletonhelper.instance; } private static class Singletonhelper { private static final Innerstatichelperclasssingleton instance = new Innerstat Ichelperclasssingleton (); } public static void Main (string[] args) { }}
The inner class has an instance of an external class, and when the outer class is loaded by the JVM, the inner class is not loaded, the instance of the outer class is not instantiated, and when the outer class's real invocation is given a singleton entry method, the internal class is loaded, and the singleton of the outer class is initialized once.
The advantage of this approach is that thread-safe singleton can be implemented without lock synchronization.
Enumerate a singleton to implement an enum Singleton The above singleton implementation may not guarantee that there is only one instance of a class due to reflection. The implementation of the following enumeration overcomes this problem, but this implementation lets go without delaying loading.
Package com.doctor.design_pattern.singleton;/** * @author sdcuike * * enum Singleton * Enumeration Implementation Singleton cannot delay loading resources, However, the enum value is guaranteed to be instantiated only once. And overcome the problem of reflection * * Created on August 1, 2016 PM 12:24:06 */public enum Enumsingleton { instance; Private Enumsingleton () { } public void DoSomething () { System.out.println (' Test do '); } public static void Main (string[] args) { EnumSingleton.instance.doSomething ();} }
serialization vs. Singletonwe typically use serialization when interacting with the network, and serialization breaks the rules of the singleton, and we get another instance (self-validating). To ensure that serialization does not affect the singleton rule, we generally implement the following methods:
Protected Object Readresolve () { return getinstance ();}
For specific reference: http://docs.oracle.com/javase/1.5.0/docs/guide/serialization/spec/input.html
Java Design Pattern Single-instance pattern (Singleton pattern)