单例模式

来源:互联网 发布:淘宝平台 编辑:程序博客网 时间:2024/06/05 17:18
public class Instance {    private final static Instance instance = new Instance();    public Instance getInstance() {        return instance;    }    private static Instance instance1;    public Instance getInstance1() {        if (instance1 == null) {            instance1 = new Instance();        }        return instance;    }    private static Instance instance2;    public static synchronized Instance getInstance2() {        if (instance2 == null) {            instance2 = new Instance();        }        return instance;    }    private static Instance instance4;    static {        instance4 = new Instance();    }    public static Instance getInstance4() {        return instance;    }    public static final class viewHolder {        private static final Instance INSTANCE5 = new Instance();    }    public static Instance getInstance5() {        return viewHolder.INSTANCE5;    }    private volatile static Instance instance6 = null;    public static Instance getInstance6() {        if (instance6 == null) {            synchronized (Instance.class) {                if (instance6 == null) {                    instance6 = new Instance();                }            }        }        return instance6;    }}
原创粉丝点击