o2o商城分销网站开发,网页设计如何设置字体,猪八戒做的网站怎么样,三明seoUML图#xff1a; 静态私有变量#xff08;即常量#xff09;保存单例对象#xff0c;防止使用过程中重新赋值#xff0c;破坏单例。私有化构造方法#xff0c;防止外部创建新的对象#xff0c;破坏单例。静态公共getInstance方法#xff0c;作为唯一获取单例对象的入口…UML图 静态私有变量即常量保存单例对象防止使用过程中重新赋值破坏单例。私有化构造方法防止外部创建新的对象破坏单例。静态公共getInstance方法作为唯一获取单例对象的入口。
public class Demo1 {public static void main(String[] args) {Singleton singleton1 new Singleton();Singleton singleton2 new Singleton();System.out.println(singleton1);System.out.println(singleton2);System.out.println(singleton1 singleton2);}static class Singleton {public Singleton() {}}
}
饿汉式--最简单有效唯一的缺陷在于当对象占用空间较大时可能浪费内存空间。
public class Demo2 {public static void main(String[] args) {Demo2 instance Demo2.getInstance();Demo2 instance2 Demo2.getInstance();System.out.println(instance);System.out.println(instance2);System.out.println(instance instance2);}private static final Demo2 singleton new Demo2();private Demo2() {}public static Demo2 getInstance() {return singleton;}
}
DCLDouble Check Lock懒汉式
public class Demo3 {public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Demo3 instance Demo3.getInstance();Demo3 instance2 Demo3.getInstance();System.out.println(instance);System.out.println(instance2);System.out.println(instance instance2);System.out.println(**************************************);ConstructorDemo3 declaredConstructor Demo3.class.getDeclaredConstructor();declaredConstructor.setAccessible(true);Demo3 demo3 declaredConstructor.newInstance();System.out.println(demo3);System.out.println(demo3 instance);}private static volatile Demo3 SINGLETON;private Demo3() {}public static Demo3 getInstance() {if (SINGLETON null) {synchronized (Demo3.class) {if (SINGLETON null) {SINGLETON new Demo3();}}}return SINGLETON;}
}
静态内部类 懒加载Lazy Initialization 实例仅在第一次调用 getInstance() 方法时创建这意味着如果在整个程序运行过程中单例并未被实际使用则不会创建其实例避免了不必要的内存消耗。线程安全Thread Safety JVM确保了类的静态初始化只会发生一次并且是线程安全的。静态内部类的实例化过程会被JVM自动处理并确保其原子性无需程序员显式添加同步锁。
public class Demo4 {public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Demo4 instance Demo4.getInstance();Demo4 instance2 Demo4.getInstance();System.out.println(instance);System.out.println(instance2);System.out.println(instance instance2);System.out.println(**************************);ConstructorDemo4 declaredConstructor Demo4.class.getDeclaredConstructor();declaredConstructor.setAccessible(true);Demo4 demo4 declaredConstructor.newInstance();System.out.println(demo4);System.out.println(demo4 instance);}private Demo4() {}public static class InnerClass {private static final Demo4 DEMO4 new Demo4();}public static Demo4 getInstance() {return InnerClass.DEMO4;}
}
枚举类--最大的优势就是可以防止通过反射创建新对象。初始化时机 枚举类型的实例是在类加载时由JVM统一初始化的这个过程是由JVM的类加载机制保障线程安全的。当枚举类型被首次访问时JVM会确保枚举类的所有实例都被正确地初始化且这个初始化过程只执行一次并在全局范围内保持可见。构造函数的私有化 枚举类型隐式包含了构造函数并且默认为私有不允许外部直接实例化。因此用户无法随意创建新的枚举实例确保了在整个系统中只能存在预定义的一组实例。JVM的内存模型 枚举实例一旦被创建就会存储在JVM方法区的枚举类的常量池中每个枚举值都是一个不可变的、唯一引用的对象这就从根本上杜绝了多线程环境下不同线程创建多个实例的可能性。
public enum Demo5 {INSTANCE;public Demo5 getInstance() {return INSTANCE;}public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Demo5 instance Demo5.INSTANCE.getInstance();Demo5 instance2 Demo5.INSTANCE.getInstance();System.out.println(instance);System.out.println(instance.hashCode());System.out.println(instance2);System.out.println(instance2.hashCode());System.out.println(instance instance2);System.out.println(***************************************************);Constructor?[] declaredConstructors Demo5.class.getDeclaredConstructors();for (Constructor? declaredConstructor : declaredConstructors) {Object demo5 declaredConstructor.newInstance();System.out.println(demo5);System.out.println(demo5.hashCode());System.out.println(demo5 instance);}}
}
大家如果需要视频讲解可以关注下我的B站 二、设计模式之单例模式的多种实现方式 文章转载自: http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn