当前位置:网站首页>Singleton mode (hungry and lazy)
Singleton mode (hungry and lazy)
2022-07-29 03:13:00 【Self cultivation class】
Single case Pattern
The so-called singleton design pattern , Is to take certain methods to ensure that in the whole software system , There can only be one object instance for a class , And this class only provides a method to get its object instance
Singleton mode has two ways : Hungry Chinese style Slacker type
step [ The singleton pattern - Hungry Chinese style ]
1. Privatize the constructor
2. Build directly inside the class
3. Provide a public static Method , Returns the object
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(" Safflower ");
private GirlFriend(String name){
this.name=name;
}
public static GirlFriend getInstance(){
return girlFriend;
}
@Override
public String toString() {
return "GirlFriend{" +
"name='" + name + '\'' +
'}';
}
}
Running results

step [ Single case Pattern - Slacker type ]
1. Privatize the constructor
2. Build directly inside the class
3. Provide a public static Method , Returns the object
4. Slacker type , Only when the user uses getInstance when , To return the object , When called again later , The last called object will be returned
So as to ensure the singleton
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(" Invoking Constructors ");
this.name=name;
}
public static Cat getInstance(){
if(cat==null){
cat=new Cat(" Cutie ");
}
return cat;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}Running results

The difference between the hungry and the lazy
1. The main difference between the two is the timing of creating objects : Hungry Chinese style is to create an object instance when the class is loaded , Lazy style is created when used
2. There is no thread safety problem in starving Han style , Lazy has thread safety issues , It will be improved after the thread
3. There is the possibility of wasting resources . Because if a programmer doesn't use an object instance , Then the objects created in a hungry way are wasted , Lazy is created when used , There is no such thing
4. In us javaSE Standard class ,java.lang.Runtime It's the classic singleton pattern
边栏推荐
- 【C】数组
- [QNX hypervisor 2.2 user manual]9.11 RAM (under update)
- Incremental real-time disaster recovery notes
- MySQL operation database data error: fatal error encoded during command execution
- Shell programming specifications and variables
- Typescript learning (I)
- What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
- Detailed steps for installing MySQL 8.0 under Linux
- HTB-Blocky
- 13_ UE4 advanced_ Montage animation realizes attack while walking
猜你喜欢

Digital image processing Chapter 10 - image segmentation

C语言基础知识点汇总

Alibaba Sentinel - workflow and principle analysis

【C】数组

Flask creation process day05-06 creation project

STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)

Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)

Multi table (Association) query of SQL query data

逐步分析类的拆分之案例——五彩斑斓的小球碰撞

照片比例校正工具:DxO ViewPoint 3 直装版
随机推荐
Unity game special effects
Server operation management system
Shell编程规范与变量
mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
12_ue4进阶_换一个更好看的人物模型
Redis configuration cache expiration listening event trigger
"PHP Basics" output approximate value of PI
C陷阱与缺陷 第3章 语义“陷阱” 3.9 整数溢出
C和指针 第3章 语义“陷阱” 3.5 空指针并非字符串
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
【机器人学习】机械臂抓手matlab运动学与admas动力学分析
照片比例校正工具:DxO ViewPoint 3 直装版
shell脚本总结
生产部署zabbix5.0笔记
[robot learning] matlab kinematics and ADMAS dynamics analysis of manipulator gripper
Introduction and advanced MySQL (XIV)
正则表达绕过waf
C traps and defects Chapter 3 semantic "traps" 3.1 pointers and arrays
Algorithm --- paint the house (kotlin)
Navicat new database