惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

浮华生

Elasticsearch 检索性能优化 - 浮华生 舆情监控系统综述 - 浮华生 2024 半年度总结 - 浮华生 2023 年终总结 - 浮华生 异地机器组网方案 - 浮华生 Kubernetes 部署 Elasticsearch 和 Kibana - 浮华生 2022 年终总结 - 浮华生 RabbitMQ connection channel 的关系 - 浮华生 Kafka Java 客户端 Producer 原理分析 - 浮华生 RabbitMQ 和 Kafka 应用原理简单对比 - 浮华生 阿里云 OpenSearch 介绍 - 浮华生 Golang Array 和 Slice 区别 - 浮华生 Mac OS 下打造 golang nvim 编程环境之基础配置 - 浮华生 电商搜索技术总结 - 浮华生 电商搜索业务总结 - 浮华生 2021 年终总结 - 浮华生 Cypress 实践总结 - 浮华生 年终总结 - 浮华生 关于我 - 浮华生 使用 cucumber 进行行为驱动开发(BDD) - 浮华生 微服务应用集成 SpringCloud 步骤 - 浮华生 电商搜索数据同步方案 - 浮华生 通过一道数值转换题重温计算机补码 - 浮华生 macOS 系统推荐的一些软件 - 浮华生 DevOps 实施规划(持续更新) - 浮华生 rabbitmq 如何提高可靠性并保证消费端幂等 - 浮华生 AMQ Model总结 - 浮华生 结对编程 - 浮华生 RSocket 介绍 - 浮华生 面向对象的理解 - 浮华生 企业平台技术框架 - 浮华生 对创业的思考 - 浮华生 知难行易 - 浮华生 一年工作经验总结 - 浮华生 我与领域驱动之缘 - 浮华生 TDD 中使用的工具 - 浮华生 tf-idf 算法 - 浮华生 gitlab 添加代码规范检测 - 浮华生 query 改写 - 浮华生 阶段性总结 - 浮华生 操作系统作用 - 浮华生 - 浮华生 hamming-distance - 浮华生 ElasticSearch API 基本操作 - 浮华生 elasticsearch 集群容错 - 浮华生 ElasticSearch 基础概念 - 浮华生 技术选型怎么做 - 浮华生 条件概率、全概率与贝叶斯公式 - 浮华生 年终总结 - 浮华生 迁移到 ubuntu18 的问题及配置 - 浮华生 总结 - 浮华生 使用 psi-probe 监控 Tomcat - 浮华生 Tomcat 远程 Debug - 浮华生 jstack 死循环和死锁定位 - 浮华生 jmap & mat 内存溢出 - 浮华生 JVM 常用参数查看 - 浮华生 周总结(8.13-8.19) - 浮华生 周总结(7.30-8.4) - 浮华生 使用移位运算符 - 浮华生 master 公式 - 浮华生 VMware 12 NAT网络下配置 ubuntu 16.04 LTS 系统静态 IP - 浮华生 关于进制的计算 - 浮华生 项目总结 第三篇 - 浮华生 项目总结 第二篇 - 浮华生 editor.md 富文本编辑器的使用 - 浮华生 项目总结 第一篇 - 浮华生 2017至今总结 - 浮华生 谈谈微服务 - 浮华生 tor 使用 - 浮华生 归档 - 浮华生 搜索 - 浮华生 搜索 && 推荐 - 浮华生
单例模式 - 浮华生
浮华生 · 2017-10-09 · via 浮华生

单例模式Singleton 作用:保证整个应用程序中某个实例有且只有一个 类型:饿汉模式和懒汉模式

首先创建一个Pattern类,什么都不写。

1
2
3
4
5
package top.richmanroad.demo;

public class Pattern {

}

再新建一个测试Test类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package top.richmanroad.demo;

public class Test {
public static void main(String[] args) {
    
    Pattern pt1=new Pattern();
    Pattern pt2=new Pattern();
    Pattern pt3=new Pattern();
    System.out.println(pt1);
    System.out.println(pt2);
    System.out.println(pt3);
}
}

控制台输出结果

1
2
3
top.richmanroad.demo.Pattern@15db9742
top.richmanroad.demo.Pattern@6d06d69c
top.richmanroad.demo.Pattern@7852e922

可以看出其三次输出的地址不相同,说明此时创建了三个不同的实例

饿汉模式

将构造方法私有化,不能被外部直接创建对象,此时在Test类中就不能直接实例化对象了,然后在Pattern类中创建唯一的实例,并且将其变为类的成员(static将方法变为类所有),这样在Test类中可以直接使用;类名.成员名来获取对象。此时Pattern类如下:

1
2
3
4
5
6
7
8
package top.richmanroad.demo;

public class Pattern {
//将构造方法私有化,不能被外部直接创建对象
    private Pattern(){}
//创建类的唯一实例
    static Pattern instance=new Pattern();
}

Test类:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package top.richmanroad.demo;

public class Test {
public static void main(String[] args) {
    
    Pattern pt1=Pattern.instance;
    Pattern pt2=Pattern.instance;
    Pattern pt3=Pattern.instance;
    System.out.println(pt1);
    System.out.println(pt2);
    System.out.println(pt3);
}
}

控制台输出结果:

1
2
3
top.richmanroad.demo.Pattern@15db9742
top.richmanroad.demo.Pattern@15db9742
top.richmanroad.demo.Pattern@15db9742

可以看出三者的地址指向一处,说明此时只创建了一个实例。

接下来为了安全,需要将对Pattern类进行封装(面向对象思想),此时我们的Pattern类就变成了如下内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package top.richmanroad.demo;

public class Pattern {
//将构造方法私有化,不能被外部直接创建对象
    private Pattern(){}
//创建类的唯一实例
 private static Pattern instance=new Pattern();
//提供一个用于获取实例的方法
 public static Pattern getInstance(){
     return instance;
 }
    
}

Test类修改内容并输出,结果同上

1
Pattern pt1=Pattern.getInstance();

上面这种方法为饿汉模式,意思大致为:static为静态修饰符,当Pattern类加载时它就会创建一个唯一的实例,不管后面有没有调用,都随类一起产生。

懒汉模式

LazyPattern类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package top.richmanroad.demo;

public class LazyPattern {
    //构造方法私有化
private LazyPattern(){
}
//声明类的唯一实例,使用private修饰
private static LazyPattern instance;
//提供一个用于获取实例的方法,使用public static修饰
public static LazyPattern getInstance() {
    if(instance==null){
        instance=new LazyPattern();
    }
    return instance;
}

}

类中只是声明了一个唯一的实例,并没有创建,只有当用户获取实例时才去判断这个实例是否为空

饿汉模式懒汉模式
区别加载时速度慢,运行时获取对象比较快加载速度快,运行时获取对象速度比较慢
线程安全不安全