当前位置:网站首页>【创建型模式】单例模式
【创建型模式】单例模式
2022-06-11 15:18:00 【Evader1997】
单例模式
单例模式,顾名思义就是单一实例。在Java中可以理解为一个类只有一个实例(对象),并且该类提供获取该实例(对象)的方法。
一个疑问

上图是Java中的两个工具类,Arrays跟Collections。从来没有接触过单例的同学,看了上图内心毫无波澜,往下看即可。接触过单例的同学带着这个问题看下面的内容:单例模式要求显式定义一个无参的构造函数,Arrays跟Collections都有私有的无参构造,为什么它们不设计成单例吗?
单例模式的特点
- 有且只有一个私有的无参构造函数(小白看下图,老司机跳过)

- 该类对外会提供一个获取该单例的方法
常见单例模式
饿汉式(立即加载)
饿汉式,我自己记忆时也记‘急汉式’,联想时就体现在急上,急就会先实例化对象,实际饿正是此意,不过急有助于记忆。
public class HungrySingleton {
// 一定义就调用构造函数创建对象
private static HungrySingleton singleton = new HungrySingleton();
private HungrySingleton() {
}
public HungrySingleton getSingleton() {
return singleton;
}
}
懒汉式(延时加载)
懒汉式与饿汉式是相对的,也是属于不急的那种,所以定义时没有立即初始化,而是使用的时候才会初始化。关于饿汉式本文只介绍常用的写法(具体这么写的原因会有文章深挖),双检锁写法(对声明的单例进行两次判null操作,意为双检)。
public class LazySingeton {
private volatile static LazySingeton singeton;
private LazySingeton() {
}
public LazySingeton getSingeton() {
if (singeton == null) {
synchronized (LazySingeton.class) {
if (singeton == null) {
// 使用时才初始化
singeton = new LazySingeton();
}
}
}
return singeton;
}
}
单例模式的应用场景
生成唯一序列号
// 不适用于分布式 public class SingetonGenerator{ // 序列化 private long id; private static final SingetonGenerator singeton = new SingetonGenerator(); private SingetonGenerator(){ } public synchronized long next(){ return ++id; } public static SerialGenerator getInstance(){ return singeton; } }// 客户端调用 public class SingetonGeneratorTest { public static void main(String[] args) { SingetonGenerator singeton = SingetonGenerator.getInstance(); // 执行这个方法后id就会增加1 singeton.next(); } }在整个项目中需要一个共享数据
很多游戏都能看见xxx玩家同时在线,比如我的青春,当时800万勇士同时在线。这个800万就是一个共享数据,假设这个共享数据定义为变量count,那么勇士登录后,就要拿到整个count进行+1操作
创建该对象消耗大量资源,最常见的就是数据库连接池,再比如Mybatis创建SQLSessionFactory,这个工厂要几个,一个即可。
类中需要有大量的静态方法,静态属性等(这其实是最开始提出的疑问,往下看)
静态方法 CF 单例模式(CF代表比较)
请大家重新看下文章开头的图片以及单例模式的特点,你会发现Arrays与Collections都具备了单例模式的第一个条件,有且只有一个私有的无参构造,但是他们都不具备第二个条件:该类对外会提供一个获取该单例的方法。为什么这两个类不设计成单例模式呢?
先说结论,静态方法是不需要变的,单例模式是需要变的,如何理解呢?类似于Arrays,Collections这样的工具类他们声明的变量与方法都是不变的(变量加final),而单例模式是需要提供一个全局访问的节点或数据的,它是需要变的,比如唯一序列号,网上在线人数,这个是静态方法做不了的。
总结
Java中很多工具类也会采用私有化的构造方法来保证实例的单一性,但它与单例还是有很多不同的,工具类是不保存状态的,它含有的只是一些静态方法或者静态属性,来供开发者使用,而单例模式如上所说是有状态的,是需要变化的。工具类只是装一些静态方法,静态属性,而单例却是有着唯一的对象实例!
单例模式最核心的点就在于单,单身狗的单,它只产生单(一)个对象。单例模式最常见的两种模式就是懒汉式和饿汉式,其实单例模式的实现方式不止本文介绍的两种。比如还有静态内部类延时加载实现单例,枚举单例等,使用时建议结合使用场景去选择懒汉或者饿汉,静态内部类与枚举单例不推荐,知道有这两种方式即可。
边栏推荐
- CNCF survey in 2021: a year for kubernetes to cross the gap
- NVIDIA R & D director: how does AI improve chip design?
- 以 Log4j 为例,如何评估和划分安全风险
- 19. insertion, deletion and pruning of binary search tree
- PowerShell主架构师:我用业余时间开发项目,表现优秀反而被微软降级了
- 06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
- 浅谈居家办公后的感想| 社区征文
- 百度某离职员工跳槽字节被判赔107万元;苹果谷歌微软拟“干掉”密码;传吉利已收购魅族|Q资讯
- 新华三交换机系统基本配置命令
- Implementation of the function of recording login status
猜你喜欢

Station B executives interpret the financial report: the epidemic has no impact on the company's long-term development, and the video trend is irresistible

06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?

Tencent interviewers share their interview experience, how to evaluate the interviewers' technical and personal comprehensive quality, and give you some suggestions on the interview

Analysis on the architecture of distributed systems - transaction and isolation level (multi object, multi operation) Part 2

Analyse approfondie de la conception du système relationnel du Groupe de cercles

Qcustomplot 1.0.1 learning (3) - plotting quadratic functions
![[SystemVerilog interface] ~ interface](/img/dc/0a9750cace1460af772e2f3f6a8763.png)
[SystemVerilog interface] ~ interface

High number_ Chapter 6 infinite series__ Marklaurin series
![[multi thread performance tuning] what operations cause context switching?](/img/a6/5d82c81dba546092447debebf7fc3e.jpg)
[multi thread performance tuning] what operations cause context switching?

Ali, tell me about the application scenarios of message oriented middleware?
随机推荐
Flower shop window (linear DP)
【SystemVerilog 之 验证】~ 测试平台、硬件设计描述、激励发生器、监测器、比较器
How about art plus online school? Is it a new online organization?
19. Insertion et suppression d'un arbre de recherche binaire
Taking log4j as an example, how to evaluate and classify security risks
01Tekton 初探
Backtracking / activity scheduling maximum compatible activities
Turning "passive" into "active", how to build security compliant intelligent products | Q recommendation
【SystemVerilog 之 接口】~ Interface
System. out. What should I pay attention to when using the println () method
Recyclerview usage record
Managing technology debt in a microservice architecture
腾讯面试官分享面试经验,如何考察面试者技术及个人综合素质,给正在面试的你一点建议
Nexus of repository manager
Basic configuration command of Xinhua 3 switch system
Leetcode daily question - Search insertion position
Implementation of gray-scale publishing scheme for microservice architecture based on gateway and Nacos
MySQL user authority summary [user authorization required]
Dynamically set the layoutinflater of the layout
多云安全合规扫描平台之RiskScanner