Optimizing the singleton design

Singleton

Definition: object creation mode, a system to ensure that only one instance of the class is generated

The benefits of using a single example:

1): for frequently used objects, omitting create objects takes time

2): reducing the number of new operations, the frequency of use of the system memory will be reduced, reducing the pressure GC, GC pause times shortened.

Create a singleton

The first way): a class loader is created

public class Singleton {
    /**
    私有化构造方法,外界不能通过new运算符创建对象
     */
    private Singleton(){
        System.out.println("create singleton");
    }

    /**
     * 实例化Singleton对象,static修饰,在类加载时实例化
     */
    private static Singleton instance = new Singleton();

    /**
     * 提供一个接口,外界可以通过该接口获取Singleton对象
     */
    public static Singleton getInstance(){
        return instance;
    }
}

Drawbacks: When using a single class of embodiments, with or without using the singleton object, singleton variables are instantiated by the new operator.

public class Singleton {
    /**
    私有化构造方法,外界不能通过new运算符创建对象
     */
    private Singleton(){
        System.out.println("create singleton");
    }

    /**
     * 实例化Singleton对象,static修饰,在类加载时实例化
     */
    private static Singleton instance = new Singleton();

    /**
     * 提供一个接口,外界可以通过该接口获取Singleton对象
     */
    public static Singleton getInstance(){
        return instance;
    }

    public static void getString(){
        System.out.println("ceateString in Singleton");
    }
}

//执行Singleton.getString()程序输出
create singleton
ceateString in Singleton
//不管有没有使用单例对象,单例变量都会通过new运算符实例化

The second way): delay loading

Single variable declarations first embodiment, when the object using only a single embodiment, it creates an instance of the new operator is null

public class LazySingleton {
    /**
    声明私有的构造器
     */
    private LazySingleton(){
        System.out.println("create lazySingleton");
    }

    /**
     * 单例变量初始化时不进行实例化
     */
    private static LazySingleton instance = null;

    /**
     * 1)提供获取单例对象的接口
     * 2)加锁:
     *   防止线程1在未赋值前,线程2可能判断instance == null,此时会产生两个单例对象
     * @return
     */
    public static synchronized LazySingleton getInstance(){
        if(instance == null){
            instance = new LazySingleton();
        }
        return instance;
    }
}

Drawbacks: multi-threading may occur when you call two singleton object, use the sychonized can prevent the phenomenon, but consumption is much larger than the first model

The third way): use private static class members inside

public class StaticSingleton {
    /**
     * 将构造方法私有化
     */
    private StaticSingleton(){
        System.out.println("create staticSingleton");
    }

    /**
     * 创建静态成员内部类,在内部类中创建单例对象
     *  --:静态成员内部类在外部类加载时不会加载,只有在调用时才会加载
     */
    private static class SingletonHolder{
        private static StaticSingleton instance = new StaticSingleton();
    }

    /**
     * 提供获取单例的接口
     *  ---:满足了只有在使用单例时才使用new进行实例化,也不必使用synchonized关键字
     */
    public static StaticSingleton getInstance(){
        return SingletonHolder.instance;
    }

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

Benefits: solve the problem of delayed loading, single cases can be created in use, do not use the keyword synchonized

Guess you like

Origin www.cnblogs.com/Auge/p/11536615.html