当前位置:网站首页>单例模式(饿汉式 懒汉式)
单例模式(饿汉式 懒汉式)
2022-07-29 02:51:00 【修身课】
单例模式
所谓的单例设计模式,就是采取一定的方法保证在整个软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法
单例模式有两种方式:饿汉式 懒汉式
步骤[单例模式-饿汉式]
1.将构造器私有化
2.在类的内部直接构建
3.提供一个公共的static方法,返回对象
public class Ch01 {
public static void main(String[] args) {
GirlFriend instance1 = GirlFriend.getInstance();
GirlFriend instance2 = GirlFriend.getInstance();
System.out.println(instance1);
System.out.println(instance1==instance2);
}
}
class GirlFriend{
private String name;
private static GirlFriend girlFriend=new GirlFriend("小红花");
private GirlFriend(String name){
this.name=name;
}
public static GirlFriend getInstance(){
return girlFriend;
}
@Override
public String toString() {
return "GirlFriend{" +
"name='" + name + '\'' +
'}';
}
}
运行结果

步骤[ 单例 模式-懒汉式]
1.将构造器私有化
2.在类的内部直接构建
3.提供一个公共的static方法,返回对象
4.懒汉式,只有当用户使用getInstance时,才返回对象,后面再次调用时,会返回上次调用的对象
从而保证的了单例
public class Ch02 {
public static void main(String[] args) {
System.out.println(Cat.age);
System.out.println(Cat.getInstance());
System.out.println(Cat.getInstance());
}
}
class Cat{
public static int age=2;
private String name;
private static Cat cat;
private Cat(String name){
System.out.println("调用构造器");
this.name=name;
}
public static Cat getInstance(){
if(cat==null){
cat=new Cat("小可爱");
}
return cat;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}运行结果

饿汉式和懒汉式的区别
1.二者最主要的区别在于创建对象的时机不同:饿汉式是在类加载就创建了对象实例,而懒汉式是使用时才创建
2.饿汉式不存在线程安全问题,懒汉式存在线程安全问题,在线程后会完善
3.饿汉式存在浪费资源的可能。因为如果程序员一个对象实例都没有使用,那么饿汉式创建的对象就浪费了,懒汉式是使用时才创建,就不存在这个问题
4.在我们javaSE标准类中,java.lang.Runtime就是经典的单例模式
边栏推荐
- C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
- Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
- Interpreting AI robots' pet raising and leading fashion trends
- MySQL 操作数据库数据报错:Fatal error encountered during command execution
- C语言程序设计 | 交换二进制数奇偶位(宏实现)
- Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers
- 金山云回港上市:中国TO B云厂商的港股奔袭
- 【FreeSwitch开发实践】UniMRCP编译与安装
- C language: hollow square pattern
- 数据截断及估计
猜你喜欢

12_ue4进阶_换一个更好看的人物模型

Interpretation of ue4.25 slate source code

创客教育的起源和内涵的基本理念

【FreeSwitch开发实践】UniMRCP编译与安装

Analysis of Project-based Learning Creativity in steam Education

sqlilabs less-32~less-33

OSPF experiment

Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers

13_ue4进阶_蒙太奇动画实现一边走一边攻击

C language: judging letters
随机推荐
Comic algorithm_ Xiaohuihui interview
Chapter 09_ Use of performance analysis tools
创客教育的起源和内涵的基本理念
Summary of common hooks
Self organization is the two-way rush of managers and members
Trample --- discretization + tree array + difference
Notes on the seventh day
FTP protocol details
10. Writing rules - Document Search
Zone --- line segment tree lazy marking board sub problem
Mysql复合查询(重要)
常用hooks总结
DHCP protocol detailed analysis
C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
Interpreting AI robots' pet raising and leading fashion trends
Add a row to a specific location in the dataframe
Analyzing the subjective consciousness of emotional resonance between robots and human beings
11. Writing rules - pseudo target
C language: Little Lele and Euclid
(job) C language: Simulation Implementation of ATOI and strncpy, strncat, strncmp