Example 2. Single Mode

I. Introduction singleton

  • A class can only be one instance of an object

II. Eight kinds of implementations

  1. Hungry man type (static constants)

  2. Starving formula (static code block)

  3. Lazy man's (thread safe)

  4. Lazy man's (thread-safe, synchronization method)

  5. Lazy formula (thread-safe, synchronized block)

  6. Double-checking

  7. Static inner classes

  8. enumerate

1. starving formula (static constants)

// starving formula (static variables) 
public  class SigletonType01 {

    // 1. privatization constructor, not outside new new 
    Private SigletonType01 () {

    }

    // 2. The class object instance is created internally 
    Private  Final  static SigletonType01 instance = new new SigletonType01 ();

    // 3. provides a public static method returns the object instance 
    public  static SigletonType01 the getInstance () {
         return instance;
    }

}

test:

@Test
public void test01(){

    SigletonType01 sigletonType01 = SigletonType01.getInstance();
    SigletonType01 sigletonType011 = SigletonType01.getInstance();
    System.out.println(sigletonType01 == sigletonType011);
    System.out.println(sigletonType01.hashCode());
    System.out.println(sigletonType011.hashCode());
    
    /*
    result:
        true
        1568059495
        1568059495
    */
}

2. starving formula (static code block)

// starving formula (static code block) 
public  class SigletonType02 {

    // 1. privatization constructor, not outside new new 
    Private SigletonType02 () {

    }

    // 2. The block of code creates an instance of a static 
    Private  Final  static SigletonType02 instance;
    
    static{
        instance = new SigletonType02();
    }

    // 3. provides a public static method returns the object instance 
    public  static SigletonType02 the getInstance () {
         return instance;
    }

}

3. The lazy man's (thread safe)

// (thread safe) lazy man's 
public  class SigletonType03 {

    // 1. privatization constructor, not outside new new 
    Private SigletonType03 () {

    }

    // 2. The block of code creates an instance of a static 
    Private  static SigletonType03 instance;

    // 3. provides a public static method returns the object instance 
    public  static SigletonType03 the getInstance () {
         IF (instance == null ) {
            instance = new SigletonType03();
        }
        return instance;
    }

}

4. lazy formula (thread-safe, synchronization method)

// lazy man's (thread-safe, synchronization code) 
public  class SigletonType04 {

    private SigletonType04(){

    }

    private static SigletonType04 instance;

    // the synchronized synchronization code, solve the problem of unsafe thread 
    public  static  the synchronized SigletonType04 getInstance () {
         IF (instance == null ) {
            instance = new SigletonType04();
        }
        return instance;
    }
}

The lazy formula (thread-safe, synchronized block)

public class SigletonType05 {

    private SigletonType05(){

    }

    private static SigletonType05 instance;

    // the synchronized sync block 
    public  static SigletonType05 the getInstance () {
         IF (instance == null ) {
             the synchronized (SigletonType05. Class ) {
                instance = new SigletonType05();
            }
        }
        return instance;
    }
}

6. Double-check

public  class SigletonType06 {

    private SigletonType06(){

    }

    private static volatile SigletonType06 instance;
    
    public static SigletonType06 getInstance(){
        if(instance == null){
            synchronized (SigletonType06.class){
                if(instance == null){
                    instance = new SigletonType06();
                }
            }
        }
        return instance;
    }
}

7. static inner classes

public  class SigletonType07 {

    private SigletonType07(){

    }

    public static class SigletonInstance{
        private static final SigletonType07 instance = new SigletonType07();
    }

    public static SigletonType07 getInstance(){
        return SigletonInstance.instance;
    }
}

8. Enumeration

enum  SigletonType08 {

    INSTANCE;

    public void method(){
        System.out.println("method");
    }
}

.JDK three examples using singleton

 

 IV. Singleton notes and detailed instructions

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12452647.html