Review: Singleton singleton design

First, what is the singleton

  Singleton pattern is one of the simplest design pattern form. The purpose of this model is that an object becomes the only instance of class System. To achieve this, it can be instantiated from the client to start. Hence the need for a mechanism to allow only generate a unique instance of an object class, a "block" access to all the objects you want to generate. The method of using the plant to limit the instantiation process. This method should be static method (class method), because let another instance of the class to generate a unique instance meaningless.

 

Second, what is the use singleton

  • 1, requires the production of a unique serial number.
  • 2, WEB counters, do not always have time to refresh, single cases of congenital cached in the database Riga.
  • 3, an object needs to create excessive consumption of resources, such as I / O connections to the database and so on.

 

Third, how to design a singleton

  analysis:

  1, only one instance of a class - constructor privatization

  2, it must create their own class - the class contains a static variable to hold this unique instance

  3, it must provide its own class on the overall system - (1) direct exposure, (2) obtaining a static get method

  

  create:

  Starving type: directly create objects, thread-safety problem does not exist

  1, directly instantiated (simple and intuitive)

  

/ ** 
 * Create initialization in the class instance object directly, regardless of whether that object creates 
 * 
 * (1) privatization constructor 
 * (2) creates and saved with the static variable 
 * (3) provided outwardly in this example 
 * ( 4) stressed that this is an example, with the final modification 
 * / 
public  class Singleton1 { 

    Private  final  static Singleton1 iNSTANCE = new new Singleton1 ();
     Private Singleton1 () { 
    } 

}

 

  2, enumeration type (most succinct)

  

/ ** 
 * enumerated type that indicates the object type is a limited number of 
 * limited to one, it would be a singleton 
 * / 
public  enum Singleton2 { 
    INSTANCE 
}

 

  3, static code block (for example of the complex)

public  class Singleton3 { 

    Private  Final  static Singleton3 INSTANCE;
     static { 
        INSTANCE = new new Singleton3 ();
         // then this data can be read some initialization, external files, configuration files, etc.
         // the Properties Property new new = the Properties (); 
    } 

    Private Singleton3 ( ) { 
    } 

}

 

 

  Lazy type: Delay create objects

  1, thread-safe (suitable for single-threaded)

** 
 * delay create an instance
  * (1 ) privatization constructor
  * (2 ) with a static variable that holds the only instance
  * (3 ) provides a static method of obtaining this instance of the object
  * / public class Singleton4 { Private static Singleton4 iNSTANCE; Private Singleton4 () { 
    } public static Singleton4 getInstance () throws InterruptedException {
         // if the instance variable is empty, you create an instance, or directly return to this example IF (iNSTANCE == null ) { 
            Thread.sleep ( 2000 ); 
            INSTANCE = new new 
        }

 

     

    

     
         Singleton4 ();
        return INSTANCE;
    }

}

 

  2, thread-safe (suitable for multi-threaded)

/ ** 
 * Create instance object delay 
 * (1) privatization constructor 
 * (2) with a static variable that holds the only instance 
 * (3) provides a static method of obtaining this instance of the object 
 * / 

public  class Singleton5 { 

    Private  static iNSTANCE Singleton5; 

    Private Singleton5 () { 
    } 

    public  static Singleton5 the getInstance () throws InterruptedException {
         IF (iNSTANCE == null ) {
             the synchronized (. Singleton5 class ) {
                 // , an instance is created if the instance variable is empty, otherwise, returns directly to the example 
                IF (iNSTANCE == null ) { 
                    the Thread.sleep (2000);
                    INSTANCE = new Singleton5();
                }
            }
        }
        return INSTANCE;
    }

}

 

  3, static inner classes (for multi-threaded)

/ ** 
 * The internal class is loaded and initialized, the only instance of an object is created INSTANCE 
 * static inner classes are not as loaded and initialized from outside the class is initialized, it is to be loaded and initialized separately, are thread-safe 
 * / 
public  class Singleton6 { 



    Private Singleton6 () { 
    } 

    Private  static  class Inner {
         Private  Final  static Singleton6 INSTANCE = new new Singleton6 (); 
    } 

    public  static Singleton6 the getInstance () {
         return Inner.INSTANCE; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/noperx/p/11314612.html