Singleton (Singleton) : JAVA is the simplest design pattern, belonging schema created. The so-called single embodiment, and that the entire program has only one instance.
Features :
Constructor privatization
In this class to instantiate an object class attribute as the present
Provide external access to this method a class object
Starving type: loads the object class loader
Scenario : small objects, with frequent, high concurrency
Features : thread-safe, more commonly, but prone to waste, affect performance, because the start initialization.
. 1 class the Singleton { 2 // constructor privatization . 3 Private the Singleton () { . 4 System.out.println ( "constructor" ); 5 } 6 // object is initialized when the class is loaded . 7 Private static Final the Singleton instance = new new the Singleton (); 8 // provide external access methods . 9 public static the Singleton the getInstance () { 10 return instance; . 11 } 12 is }
Lazy style: when an object is created when needed, thread safe
Scenario: single-threaded, large objects
Features: thread unsafe lazy initialization.
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 private static Singleton instance; 6 public static Singleton getInstance() { 7 if (instance == null) { 8 instance = new Singleton(); 9 } 10 return instance; 11 } 12 }
Synchronization lock mechanism
Scenario: multi-threading, large objects, rare use.
Features: by locking to ensure thread safety, performance degrades.
. 1 class the Singleton { 2 Private the Singleton () { . 3 System.out.println ( "constructor" ); . 4 } . 5 Private static the Singleton instance; . 6 // synchronization method, thread safe, but the performance will be degraded . 7 public static the synchronized the Singleton the getInstance () { . 8 IF (instance == null ) { . 9 instance = new new the Singleton (); 10 } . 11 return instance; 12 is } 13 is }
Two-factor authentication
Scenario: large objects, with rare, complicated by the amount is not too large
Features: thread-safe, lazy initialization.
. 1 class the Singleton { 2 Private the Singleton () { . 3 System.out.println ( "constructor" ); . 4 } . 5 Private static volatile the Singleton instance; . 6 // synchronization method, two-factor authentication, reducing the number of blocking, to improve performance . 7 public static the getInstance singleton () { . 8 IF (instance == null ) { . 9 the synchronized (singleton. class ) { 10 IF (instance == null ) { . 11 instance = new new Singleton(); 12 } 13 } 14 } 15 return instance; 16 } 17 }
Static inner classes
Reference scenario: a large object, with frequent, high concurrency
Features: delay object creation, reduce resource consumption, improve system performance
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 static class Inner{ 6 private static final Singleton instance = new Singleton(); 7 } 8 public static Singleton getInstance() { 9 return Inner.instance; 10 } 11 }
enumerate
. 1 enum the Singleton { 2 // created when the class loader . 3 INSTANCE; . 4 }
Since the Singleton pattern is to create a schema, each call will create a new instance. So an important question is deserialized. When the instance is written to the file when deserialized into instances, we need to override readResolve
the method to make instances unique.
1 private Object readResolve() throws ObjectStreamException{ 2 return singleton; 3 }