Java行业情景分析_Java 设计模式情景分析——单例模式

单例模式可以说是应用最广的模式之一,在应用单例模式时,单例对象的类必须保证只有一个实例存在,而且可以自行实例化并向整个系统提供这个实例。一般在不能自由构造对象的情况下,就会使用单例设计模式,例如创建一个对象需要消耗资源过多,还有访问 IO 和数据库等资源的情况。

1.单例模式

单例模式

优点

1、在内存中只有一个实例,减少了内存开支和性能开销;

2、可以避免对同一资源文件的同时写操作;

3、可以在系统设置全局的访问点,优化和共享资源访问,例如可以设计一个单例类,负责所有数据表的映射处理;

缺点

1、一般没有接口,扩展困难;

2、单例模式如果持有 Context,很容易引发内存泄漏,此时需要注意传递给单例对象的 Context 最好是 Application Context;

UML 类图:

a3a5d9281a1f5149f336840695ea6d3b.png

1.饿汉式单例

public class Singleton {

private static final Singleton instance = new Singleton(); // 句柄

private Singleton() {

}

public static Singleton getInstance() {

return instance;

}

}

2.懒汉式单例

优点是单例只有在使用时才会被实例化;缺点是第一次加载反应稍慢,每次调用 getInstance() 都进行同步,造成了不必要的同步开销,一般不建议使用。

public class Singleton {

private static Singleton instance; // 句柄

private Singleton() {

}

public static synchronized Singleton getInstance() { // 有同步开销

if (instance == null) {

instance = new Singleton();

}

return instance;

}

}

3.DCL单例

Double Check Lock 实现单例,优点是既能够在需要时才初始化单例,又能够保证线程安全,且单例对象初始化后调用 getInstance() 不进行同步锁;缺点是第一次加载反应稍慢。

public class Singleton {

// volatile 禁止指令重排序优化

private volatile static Singleton instance = null; // 句柄

private Singleton() {

}

public static Singleton getInstance() {

if (instance == null) { // 避免不必要的同步

synchronized (Singleton.class) {

if (instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

4.静态内部类单例(推荐使用)

第一次加载不会初始化 Singleton,只有在第一次调用 Singleton 的 getInstance() 才会初始化,第一次调用会导致虚拟机加载 SingletonHolder 类,这种方式不仅能够确保线程安全,也能够保证单例对象的唯一性,同时也延迟了单例的实例化,推荐使用。

public class Singleton {

private Singleton() {

}

public static Singleton getInstance() {

return SingletonHolder.instance;

}

private static class SingletonHolder {

private static final Singleton instance = new Singleton();

}

}

5.使用容器实现单例

在初始,将多种单例类型注入到一个统一的管理类中,在使用时根据 key 获取对象对应类型的对象。可管理多种类型的单例,并且在使用时可以通过统一的接口进行获取操作,降低了耦合度。

public class SingletonManager {

private static Map objMap = new HashMap<>();

private SingletonManager() {

}

public static void registerInstance(String key, Object instance) {

if (objMap.containsKey(key)) {

objMap.put(key, instance);

}

}

public static Object getInstance(String key) {

return objMap.get(key);

}

}

不管以何种形式实现单例模式,它们的核心原理都是将构造函数私有化,并且通过静态方法获取一个唯一的实例,在这个获取的过程中必须保证线程安全、防止反序列化导致重新生成实例对象等问题,选择何种取决于项目本身,并发环境是否复杂、单例对象等资源消耗等。

2.Spring源码中的应用情景

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440778.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php实现播放直播_PHP直播技术分享(一:实现直播)

推流服务器采用的是自搭的推流服务器 , 自己玩玩 做外包项目还是去搞七牛云/阿里这样的服务器吧,开始bb-----1:技术栈image.jpeg2:开发中业务(1)主播申请时创建个秘钥 , 这个秘钥随时字符串即可到时候根据字符串找到拉流的直播位置存数据库包括推流地址image.png3:配置nginx-rt…

php获取location,php获取header[‘location’]信息常见问题

15/01/31本文关键字: 302, header, location//初始化url信息$host “#8221;;$url$host.”l/rzTf7ap2viw/&iid222004556&resourceId0_04_05_99/v.swf”;//按照字段获取header响应信息$headers get_headers($url, TRUE);//获取这个土豆的302跳转地址$u302 $headers[“Lo…

php对联广告,html左右对联代码 cms网站对联广告html代码

实现网页左右两侧居中的对联广告代码你只记得自己有好多朋友却从没想过小编只有你一人。无标题文档 #left{ width:200px;height:450px; position:absolute; left:0px; background:red; border:1px #000 solid} #right{width:200px;height:450px; position:absolute; right:0px;…

php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构&#xff1a;http://www.xyqzmt.cn/1.html/2通常固定链接都是这样的结构&#xff0c;设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题&#xff0c;最后放弃挣扎&#xff0c;如果遇到很长的文章全都放在一个页…

【牛客 - 181F】子序列(容斥,组合数,费马小定理)

题干&#xff1a; 题目描述 给出一个长度为n的序列&#xff0c;你需要计算出所有长度为k的子序列中&#xff0c;除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T&#xff0c;表示数据组数。 对于每组数据&#xff0c;第一行两个整数N&#xff0c;k&#…

【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干&#xff1a; Lets denote as the number of bits set (1 bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, an…

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…

【CodeForces - 1020A】New Building for SIS(模拟)

题干&#xff1a; You are looking at the floor plan of the Summer Informatics Schools new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get …

【CodeForces - 1020B】Badge(模拟,图,环)

题干&#xff1a; In Summer Informatics School, if a student doesnt behave well, teachers make a hole in his badge. And today one of the teachers caught a group of nn students doing yet another trick. Lets assume that all these students are numbered from …

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…

【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/I 来源&#xff1a;牛客网 现在处女座顺利的完成了测验&#xff0c;处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。 更具体的&#xff0c…

linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

原标题&#xff1a;你可能不知道的 即时监控 Linux 使用者执行指令的三种方法这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令。Linux 的 root 管理者可对系统进行任何的管理与操作&#xff0c;如果想要即时监控特定使用者在主机上所执行的指令&am…

【POJ - 2728】Desert King (最有比率生成树,分数规划)

题干&#xff1a; David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be…

linux远程打开windows程序,为新手讲解Linux和Windows系统的远程桌面访问知识

很多新手都是使用Linux和Windows双系统的&#xff0c;它们之间的远程桌面访问是如何连接的&#xff0c;我们就为新手讲解Linux和Windows系统的远程桌面访问知识&#xff0c;包括所使用的软件及方法。本文所使用的Linux版本是深度操作系统&#xff0c;如果要安装该版本请参考U盘…

【蓝桥杯官网试题 - 算法训练 】K好数(线性dp与优化)

题干&#xff1a; 问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字&#xff0c;那么我们就说这个数是K好数。求L位K进制数中K好数的数目。例如K 4&#xff0c;L 2的时候&#xff0c;所有K好数为11、13、20、22、30、31、33 共7个。由于这个数目很大…