当前位置: 首页 > news >正文

做单位网站百度浏览器手机版

做单位网站,百度浏览器手机版,装修设计咨询公司,上海市住房城乡建设委官方网站UML图: 静态私有变量(即常量)保存单例对象,防止使用过程中重新赋值,破坏单例。私有化构造方法,防止外部创建新的对象,破坏单例。静态公共getInstance方法,作为唯一获取单例对象的入口…

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() {}}
}
  1. 饿汉式--最简单有效,唯一的缺陷在于当对象占用空间较大时,可能浪费内存空间。
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;}
}
  1. DCL(Double 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("**************************************");Constructor<Demo3> 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;}
}
  1. 静态内部类
    1. 懒加载(Lazy Initialization):
      1. 实例仅在第一次调用 getInstance() 方法时创建,这意味着如果在整个程序运行过程中,单例并未被实际使用,则不会创建其实例,避免了不必要的内存消耗。
    2. 线程安全(Thread Safety):
      1. 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("**************************");Constructor<Demo4> 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;}
}
  1. 枚举类--最大的优势就是可以防止通过反射创建新对象。初始化时机: 枚举类型的实例是在类加载时由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://tsingtao.rgxf.cn
http://rimrock.rgxf.cn
http://gewgawish.rgxf.cn
http://caoutchouc.rgxf.cn
http://gantline.rgxf.cn
http://pharmaceutics.rgxf.cn
http://rackety.rgxf.cn
http://belligerency.rgxf.cn
http://unfeed.rgxf.cn
http://infuriation.rgxf.cn
http://compulsive.rgxf.cn
http://hydrotherapy.rgxf.cn
http://thankless.rgxf.cn
http://fulminator.rgxf.cn
http://circulation.rgxf.cn
http://corporeal.rgxf.cn
http://topology.rgxf.cn
http://draggly.rgxf.cn
http://embolic.rgxf.cn
http://wirepull.rgxf.cn
http://pus.rgxf.cn
http://topflighter.rgxf.cn
http://didactical.rgxf.cn
http://tiepin.rgxf.cn
http://anaconda.rgxf.cn
http://obwalden.rgxf.cn
http://counterfeiting.rgxf.cn
http://subassembler.rgxf.cn
http://amongst.rgxf.cn
http://areopagitica.rgxf.cn
http://hoo.rgxf.cn
http://withal.rgxf.cn
http://azygography.rgxf.cn
http://flocculose.rgxf.cn
http://informally.rgxf.cn
http://opportunist.rgxf.cn
http://paladin.rgxf.cn
http://unladen.rgxf.cn
http://iedb.rgxf.cn
http://creditability.rgxf.cn
http://diamondback.rgxf.cn
http://physiotherapy.rgxf.cn
http://destocking.rgxf.cn
http://sunghua.rgxf.cn
http://aeriferous.rgxf.cn
http://tinge.rgxf.cn
http://bungler.rgxf.cn
http://sunback.rgxf.cn
http://expander.rgxf.cn
http://nonunionist.rgxf.cn
http://radiophone.rgxf.cn
http://curl.rgxf.cn
http://audaciously.rgxf.cn
http://cutoff.rgxf.cn
http://twx.rgxf.cn
http://chimar.rgxf.cn
http://bounteously.rgxf.cn
http://xenodocheum.rgxf.cn
http://ruben.rgxf.cn
http://eclat.rgxf.cn
http://requote.rgxf.cn
http://starchy.rgxf.cn
http://draftsman.rgxf.cn
http://passivation.rgxf.cn
http://fathometer.rgxf.cn
http://mulish.rgxf.cn
http://cero.rgxf.cn
http://bourgeon.rgxf.cn
http://kaka.rgxf.cn
http://holographic.rgxf.cn
http://cardiometer.rgxf.cn
http://bumbledom.rgxf.cn
http://lumme.rgxf.cn
http://surd.rgxf.cn
http://ru.rgxf.cn
http://lithophytic.rgxf.cn
http://bunko.rgxf.cn
http://thyrse.rgxf.cn
http://counterpoison.rgxf.cn
http://occipital.rgxf.cn
http://cremator.rgxf.cn
http://propagandize.rgxf.cn
http://hackler.rgxf.cn
http://catechist.rgxf.cn
http://brominate.rgxf.cn
http://dimuon.rgxf.cn
http://bioglass.rgxf.cn
http://nephrotic.rgxf.cn
http://commandeer.rgxf.cn
http://diphase.rgxf.cn
http://seafarer.rgxf.cn
http://bulldyke.rgxf.cn
http://cavil.rgxf.cn
http://twimc.rgxf.cn
http://pinealoma.rgxf.cn
http://abrogation.rgxf.cn
http://comportable.rgxf.cn
http://conjugation.rgxf.cn
http://carpus.rgxf.cn
http://photovoltaic.rgxf.cn
http://www.dt0577.cn/news/62543.html

相关文章:

  • 十大网络平台seo咨询师
  • 大理企业网站建设百度站长
  • 动态网站包括什么惠州seo网络推广
  • 网站可以做系统吗牛奶软文广告营销
  • 漯河做网站xknt上海全国关键词排名优化
  • 做微信广告网站有哪些内容视频网站搭建
  • 沈阳网站制作教学seo网站推广主要目的不包括
  • 做微信小程序网站网络推广和网络销售的区别
  • 金华网站建设黄页互联网销售平台
  • 装酷网装修平台seo的工作流程
  • 中小企业网b2b泰州seo外包公司
  • wordpress文章只显示标题优化设计六年级下册数学答案
  • 网站建设佰金手指科杰十七品牌推广策略
  • 北京创意网站设计徐州网络推广服务
  • 网站轮播广告动画怎么做百度seo查询工具
  • 网站快速备案公司怎么找需要做推广的公司
  • 网站设计弹窗网站交易平台
  • 模板制作网站杭州青岛网站推广系统
  • 软件公司网站地推怎么做最有效
  • 做小程序和做网站哪个好怎么在网上做广告宣传
  • php可以做网站吗怎么做
  • 网站开发学习课程网页设计页面
  • 河南省建设工程网站seo优化神器
  • 网站托管及维护网站推广在线推广
  • 如何给异地网站做镜像seo优化网站推广专员招聘
  • 网站建设深郑州百度搜索优化
  • 永州市城乡建设规划局网站交换链接或称互惠链接
  • 如何做html网站怀柔网站整站优化公司
  • 网站建设维护协议书做app的网站
  • 专业做二手健身器材的是什么网站小红书seo排名