当前位置:网站首页>Implementation of singleton mode
Implementation of singleton mode
2022-06-13 10:37:00 【edui】
- Definition
This type of design pattern is a creation pattern , It provides the best way to create objects .
This pattern involves a single class , This class is responsible for creating its own objects , Also make sure that only a single object is created . This class provides a way to access its unique objects , You can directly access , There is no need to instantiate an object of this class . - Realization
public class Singleton{
// A private waiting variable holds an instance of the class , Not directly accessible and modifiable
private static Singleton instance= new Singleton();
// Privatization constructor , Cannot be used by external bars to create instances
private Singleton(){
}
// The only interface gets the object of the singleton
public static Singleton getInstance(){
return instance;
}
/** * Other members of the class **/
}
First, the constructor is controlled as private , Can only be instantiated for calls within a class , In this way, other classes cannot instantiate the object .
secondly , Define a variable that holds a unique instance , Must be a class variable , Used to save unique instance objects .
Next, provide an interface to get the unique instance .
Such as ,static Decorated class variables instance In the initialization phase of class loading, you will get an instance , Keep in method area , Execute the only instance object in the heap , Because it is directly saved in the method area , It also provides a static method access method, so each time you access the instance of this class, you will get the class variables that save the method area instance value , This value points to an object in the heap , Class variables instance Value value is unique ( Because classes are unique in the method area ) So the instance is also unique .
The above method is thread safe , But as long as the class is loaded, an object will be created regardless of whether it is used or not , Waste of space is not supported Lazy initialization , So there is support Lazy Implementation method of initialization .
——————————————————————————————————————————————
public class Singleton{
// A private waiting variable holds an instance of the class , Not directly accessible and modifiable
private volatile static Singleton instance;
// Privatization constructor , Cannot be used by external bars to create instances
private Singleton(){
}
// The only interface gets the object of the singleton
public static Singleton getInstance(){
// If it does not exist during use, it will be initialized first , Because to ensure that only one object is created , Apply synchronization lock
if(instance = null){
synchronized(Singleton.class){
if(instance = null)
instance = new Singleton;
}
}
return instance;
}
/** * Other members of the class **/
}
This implementation method instantiates the class only when the object needs to be obtained , It is realized. Lazy initialization , For thread safety, that is, no matter how many threads access at the same time, only one object can be instantiated, and a synchronization lock is required , The class variables of the instance object need to use volatile Decoration ensures that other threads are visible after one thread is instantiated .
Although this method has realized Lazy Initialization, but adding a synchronization lock will affect some performance , You can't have both fish and bear
————————————————————————————————————————
Another common implementation is , By creating inner classes , The inner class is loaded only when it is used , That is, by delaying the loading of classes Lazy The function of initialization , It is thread safe again
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){
}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
边栏推荐
- IDEA远程调试spark-submit提交的jar
- C# 11 更加实用的 nameof
- C 11 more practical NAMEOF
- DNS协议分析
- 基于SSM实现水果商城批发平台
- 苹果放大招!这件事干的太漂亮了……
- Sunyuchen, head of Grenada delegation, attended the WTO MC12 and emphasized the development of digital economy
- Docker部署Mysql
- 在 Kubernetes 集群上部署 VSCode
- Vivo large scale kubernetes cluster automation operation and maintenance practice
猜你喜欢

Classical convolutional neural network model
![[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]](/img/4a/3beaeb1b55acc9e61d3b07dff56352.png)
[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]

Introduction to knowledge map

Mysql database conceptual design using E-R model

C# 11 更加实用的 nameof

实战模拟│企业微信机器人实时报错预警
![[image denoising] image denoising based on MATLAB Gaussian + mean + median + bilateral filtering [including Matlab source code 1872]](/img/8d/3c2664738ad5ab11a35b7aadc8eb88.png)
[image denoising] image denoising based on MATLAB Gaussian + mean + median + bilateral filtering [including Matlab source code 1872]

低代码开发一个基础模块

Oracle自定义数据类型Type疑问

Vivo large scale kubernetes cluster automation operation and maintenance practice
随机推荐
IDEA 续命插件
冗余码题型--后面加0的区别
中国SaaS产业全景图谱
Implementation of fruit mall wholesale platform based on SSM
ADG standby mrp0 status wait_ FOR_ GAP
Thingsboard tutorial (20): filtering telemetry data using regular chains
电解电容、钽电容、普通电容
[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]
Système de gestion des défauts du projet Cynthia
Matlab hub motor analysis fuzzy PID control vertical vibration analysis
Introduction to knowledge map
Oracle custom data type question
ADG备库MRP0状态WAIT_FOR_GAP
苹果放大招!这件事干的太漂亮了……
On the exploitation of a horizontal ultra vires vulnerability
What is 400g Ethernet?
Redis初始安装和使用【玩转华为云】
Idea remote debugging jar submitted by spark submit
逐向双碳:东数西算中的绿色需求与竞争焦点
知识图谱入门