当前位置:网站首页>【创建型模式】单例模式
【创建型模式】单例模式
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中很多工具类也会采用私有化的构造方法来保证实例的单一性,但它与单例还是有很多不同的,工具类是不保存状态的,它含有的只是一些静态方法或者静态属性,来供开发者使用,而单例模式如上所说是有状态的,是需要变化的。工具类只是装一些静态方法,静态属性,而单例却是有着唯一的对象实例!
单例模式最核心的点就在于单,单身狗的单,它只产生单(一)个对象。单例模式最常见的两种模式就是懒汉式和饿汉式,其实单例模式的实现方式不止本文介绍的两种。比如还有静态内部类延时加载实现单例,枚举单例等,使用时建议结合使用场景去选择懒汉或者饿汉,静态内部类与枚举单例不推荐,知道有这两种方式即可。
边栏推荐
- A brief talk on the feelings after working at home | community essay solicitation
- 化“被动”为“主动”,如何构建安全合规的智能产品 | Q推荐
- CNCF survey in 2021: a year for kubernetes to cross the gap
- In the "ten billion blue ocean" database, each player can find a boat | c-position face-to-face
- C语言简易版webserver
- 容易让单片机程序跑飞的原因
- LoveLive! Published an AI paper: generating models to write music scores automatically
- Explain the kubernetes package management tool Helm
- What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
- Raspberry pie obtains the function of network installation system without the help of other devices
猜你喜欢

In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology

【Azure 应用服务】NodeJS Express + MSAL 实现API应用Token认证(AAD OAuth2 idToken)的认证实验 -- passport.authenticate()

03 _ 事务隔离:为什么你改了我还看不见?

见微知著,细节上雕花:SVG生成矢量格式网站图标(Favicon)探究

uniapp開發微信小程序,從構建到上線

Implementation of gray-scale publishing scheme for microservice architecture based on gateway and Nacos

河北 黄金寨景区新增“AED自动除颤器”保障游客生命安全!

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

C语言简易版webserver

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
随机推荐
多云安全合规扫描平台之RiskScanner
After many years of digital transformation projects, the main architects are desperate: outsourcing should not have been used at the beginning!
Microservices - use of Nacos
C language simple webserver
Taking log4j as an example, how to evaluate and classify security risks
How to do well in we media? Did you do these steps right?
Tangzhengrong: CTO is the intersection of business thinking and technical thinking
Uniapp settings page Jump effect - navigateto switching effect - Global animationtype animation
A brief talk on the feelings after working at home | community essay solicitation
Why do I need the public static void main (string[] args) method?
04 _ 深入浅出索引(上)
Analyse approfondie de la conception du système relationnel du Groupe de cercles
Repository Manager之Nexus配置yum仓库
A former employee of Baidu was awarded 1.07 million yuan for job hopping; Apple, Google and Microsoft plan to "kill" the password; It is said that Geely has acquired Meizu | Q information
Art plus online school: Sketch common sitting posture test questions, these three angles must be mastered~
Zhejiang University has developed a UAV, which can automatically avoid obstacles and walk through the woods like a bird. The real swarm is coming
Architectural concept exploration: Taking the development of card games as an example
PowerShell主架构师:我用业余时间开发项目,表现优秀反而被微软降级了
Qualcomm WLAN framework learning (29) -- 6GHz overview
Tencent interviewers share their interview experience, how to evaluate the interviewers' technical and personal comprehensive quality, and give you some suggestions on the interview