sort: Java Singleton Design ultimate summary

Outline

  • Singleton The most common design patterns most frequently asked
  • Singleton design pattern (Singleton), i.e., a class in the whole system can be only one instance of
  • For example: Representative JVM runtime environment Runtime class, XxxClass representative of certain information, spring-IOC containers like bean
  • key point
    • Constructor privatization - not arbitrarily new external guarantee a new instance
    • Class itself provides this example - static variable or static method
  • Design Category
    • Examples of time can be divided by starving formula and formula lazy
    • Hungry man style is characterized by creating an instance of the class object is loaded, regardless of whether the instance will be used. The advantage is simple, thread-safe, the disadvantage is resource consumption.
    • Lazy man is delayed instance creation, is instantiated, lazy man's disadvantage is the need to pay attention to thread safety issues when in need of such instances.

Several hungry Chinese-style writing

The most classic writing

public class Eager01 {
    //构造器私有化
    private Eager01() {}
    //public + static,提供外部直接类名获取
    //final 强调单例
    public static final Eager01 INSTANCE = new Eager01();
}

Enumeration wording

  • Use enumeration singleton design now become the most elegant and respected the way, has many advantages
  • Pros: Simple and elegant writing; thread-safe; can cope with the reflection attack; serialization support ...
  • The only drawback: Because the enumerated classes, and inheritance Enum, so can not inherit expansion. Strictly speaking, this is not really the disadvantage ..
  • Writing point of view can be seen as a replica of the classic simplicity of writing
//理解:
//枚举类型即其实例为限定范围的几个,我们只提供一个,那么就是单例
public enum Eager2_enum {
    INSTANCE;
}

Static code block wording

  • It requires complex logic applicable to the scenario instantiation process, such as the loading properties files
  • These examples of processing a static block is completed in
public class Eager3_static_block {
    private Eager3_static_block() {}
    public static Eager3_static_block instance;
    static {
        //...其他操作,比如资源属性文件加载等
        instance = new Eager3_static_block();
    }
}
  • Example best to add a single final, method using private staic
public class Eager3_static_block {
    private Eager3_static_block() {}
    public final static Eager3_static_block INSTANCE = init();
//  static {
//      //...其他操作,比如资源属性文件加载等
//      INSTANCE = new Eager3_static_block();
//  }
    private static Eager3_static_block init() {
        return new Eager3_static_block();
    }
}

Several lazy writing style

Simple examples of delayed version - non-thread-safe

public class Lazy01 {
    //还是一样,构造器私有化
    private Lazy01() {}
    //静态私有变量
    private static Lazy01 instance;
    //提供public静态方法以获取实例
    public static Lazy01 getSingleton() {
        if(null == instance) {
            instance = new Lazy01();
        }
        return instance;
    }
}

Thread-safe - Static method synchronized version

public class Lazy2 {
    //还是一样,构造器私有化
    private Lazy2() {}
    //静态私有变量
    private static Lazy2 instance;
    //提供public静态方法以获取实例,静态方法加锁
    public synchronized static Lazy2 getSingleton() {
        if(null == instance) {
            instance = new Lazy2();
        }
        return instance;
    }
}

Both performance and safety - double check the synchronization lock version

public class Lazy3 {
    //还是一样,构造器私有化
    private Lazy3() {}
    //静态私有变量
    private static Lazy3 instance;
    //提供public静态方法以获取实例
    public static Lazy3 getSingleton() {
        if(null == instance) { //第一次检查,提高性能,只有第一次为null时才触发锁
            synchronized (Lazy2.class) {
                if(null == instance) { //第二次检查,安全考虑,防止线程同时通过第一次检查
                    instance = new Lazy3();
                }
            }
        }
        return instance;
    }
}

Elegant lazy man - static inner classes version

  • Enumeration using the same formula as lazy, elegant and belonging singleton design respected
  • Use a static inner classes, hungry Chinese-style interior, exterior lazy man, very clever, but also avoid the use of synchronization lock
public class Lazy4 {
    //还是一样,构造器私有化
    private Lazy4() {}
    //静态内部类
    private static class Inner {
        //饿汉式
        private static final Lazy4 INSTANCE = new Lazy4();
    }
    //外部懒汉式(调用此方法才会加载内部类,创建 INSTANCE 实例)
    public static Lazy4 getSingleton() {
        return Inner.INSTANCE;
    }
}

Guess you like

Origin www.cnblogs.com/noodlerkun/p/11578194.html