Singleton mode: Ensure that there is only one instance per class and provide a global access point to access this unique instance. (Logging,driver objects)
Implementation: (1) Private constructors restrict the instantiation of other classes
(2) private static pointer to itself member variable, class unique instance
(3) public static method that provides a global access point to access this unique instance
A. A hungry man type singleton: Create instance object when class is loaded, advantage need not consider multi-threaded simultaneous access problem, disadvantage class load slow, client does not need practical object is also created instance, resource utilization is not high.
public class singleton2 { /* * a hungry man singleton mode: Creates a singleton mode when the class is loaded . */ // 1. Private constructor, ensure that the class's constructor cannot be accessed outside the class private singleton2 () { system.out.println ("constructor executed"); } // 2. Private unique static instance variable, Create a singleton object when the class is loaded private final static Singleton2 Instance = new singleton2 (); // 3. Publicly available static factory returns the only instance of this class public static Singleton2 getinstance () { return instance; } &nbSP;}
b. Lazy Singleton: The first time (call the static factory method) call to instantiate itself, implement the lazy load, must handle the multi-threaded simultaneous access.
public class singleton { /* * Singleton mode: A single instance serves the entire application * Lazy Singleton: created only when the instance is first requested and after the first creation, no instances of the class are created at a later time */ // 1. A private static variable pointing to itself private static Singleton instance; // 2. Private construction methods to ensure that objects cannot be created externally private singleton () {} // 3. Exposes a static factory method that returns a unique instance of the class (initialized when no instance is found to be initialized) public static Synchronized singleton getinstance () { //synchronized keyword guarantees thread safety if (instance == null) { instance = new&nbSp Singleton (); system.out.println (" Create an instance of the Singleton class "); }else { system.out.println ("The instance has been created and can no longer be created"); } return instance; } }
double check and lock mechanism , refers to: not every time into the GetInstance method need to synchronize, but first out of sync, enter the method after, first check whether the instance exists, if not exist before entering the following synchronization block, this is the first check. After entering the synchronization block, check again if the instance exists, and if it does not exist, create an instance in the case of synchronization, which is the second check. In this way, you only need to synchronize once, thus reducing the
multiple times in the case of synchronization to determine the time wasted.
public class singleton{ private static singleton Whether instance = null;//is final is not important because it is possible to instantiate at most one time. private singleton () {} public static Singleton getinstance () { if (instance == NULL) { //Double check lock, only when the first instantiation is The synchronization mechanism is enabled for improved performance. synchronized (Singleton.Class) { if ( Instance == null) { instance = new singleton (); &nBSP;} } } return instance; } }
Singleton mode (Singleton)