单例模式

来源:互联网 发布:keynote for windows 编辑:程序博客网 时间:2024/06/08 00:31

单例模式很多种,但其他几种都有缺点,只需要记住下面这一种单例模式:

public class Singleton {    private volatile static Singleton uniqueInstance;    private Singleton(){    }    public static Singleton getInstance(){        if (uniqueInstance == null) {            synchronized (Singleton.class) {                if (uniqueInstance == null) {                    uniqueInstance = new Singleton();                }            }        }        return uniqueInstance;    }}
原创粉丝点击