1. Singleton implementation

  • The hungry type
  • LanHanShi
  • LanHanShi + Synchronized
  • Double check
  • Static inner class
  • Enumeration (recommended)

Two, implementation code

  • The hungry type

    package com.hanko.designpattern.singleton;

    / * *

    • Hungry (when you are hungry, you are afraid of not eating, so you come out new before you use it)

    * Advantages: Simple implementation, secure and reliable * Disadvantages: instantiated when not needed

    • @author hanko
    • @ version 1.0
    • @date 2020/9/14 18:50

    Private static HungrySingleton instance = new HungrySingleton(); private static HungrySingleton instance = new HungrySingleton(); Private HungrySingleton(){

    } public static HungrySingleton getInstance(){ return instance; } public void doSomething(){Copy the code

    }

  • LanHanShi

    package com.hanko.designpattern.singleton;

    / * *

    • Lazy (very lazy, so go new when you need to use it)

    Advantages: Simple * Disadvantages: There are thread safety issues

    • @author hanko
    • @ version 1.0
    • @date 2020/9/14 18:50

    Public Class SluggardSingleton {// Public class SluggardSingleton instance is not initialized; Private Private SluggardSingleton(){

    } // Feature three null judgment, New public Static SluggardSingleton getInstance(){if(instance == null){instance = new SluggardSingleton(); } return instance; } public void doSomething(){Copy the code

    }

  • LanHanShi + Synchronized

    package com.hanko.designpattern.singleton;

    / * *

    • Lazy (very lazy, so go new when you need to use it)

    Advantages: Simple * Disadvantages: There are thread safety issues

    • @author hanko
    • @ version 1.0
    • @date 2020/9/14 18:50

    Public Class SluggardSingleton {// Public class SluggardSingleton instance is not initialized; Private Private SluggardSingleton(){

    } // Feature three null judgment, New public static synchronized slugger getInstance(){if(instance == null){instance = new SluggardSingleton(); } return instance; } public void doSomething(){Copy the code

    }

  • Double check

    package com.hanko.designpattern.singleton;

    / * *

    • Double check

    * Thread-safe handling of lazy singletons added locking mechanism

    • A volatile variable level
    • Synchronized class level
    • @author hanko
    • @ version 1.0
    • @date 2020/9/15 9:53

    */ public class DoubleCheckSingleton { Private static Volatile DoubleCheckSingleton Instance; Private private DoubleCheckSingleton(){

    Public static doublecheckleton getInstance(){if (instance == null){synchronized public static doublecheckleton getInstance(){if (instance == null){ synchronized(DoubleCheckSingleton.class){ if (instance == null){ instance = new DoubleCheckSingleton(); } } } return instance; }Copy the code

    }

  • Static inner class

    package com.hanko.designpattern.singleton;

    / * *

    • Internal static class mode

    * Advantages: Static inner classes are not loaded when the InnerStaticSingleton class is loaded,

    • Instead, it is loaded when the getInstance() method is called

    Disadvantages: There are reflection or deserialization attacks

    • @author hanko
    • @ version 1.0
    • @date 2020/9/15 10:03

    Private InnerStaticSingleton(){// InnerStaticSingleton(){

    } private static class InnerSingleton{private static InnerSingleton instance = new InnerSingleton(); } public InnerSingleton getInstance(){ return InnerSingleton.instance; } public void doSomething(){ //do Something }Copy the code

    }

  • Enumeration (recommended)

    package com.hanko.designpattern.singleton;

    / * *

    • Enumeration implements singleton simplicity and security
    • @author hanko
    • @ version 1.0
    • @date 2020/9/14 19:01

    */ public enum EnumSingleton { INS; private Singleton singleton; EnumSingleton() { singleton = new Singleton(); } public void doSomething(){ singleton… // What you need to implement}

    }

    EnumSingleton.INS.doSomething();

The source code

[hanko/design-pattern​

gitee.com

] (link.zhihu.com/?target=htt…).