当前位置:网站首页>Singleton mode instance
Singleton mode instance
2022-06-12 15:13:00 【Aiyouwei】
The singleton pattern
- The singleton pattern (Singleton Pattern) yes Java One of the simplest design patterns in . 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 .
Aspects of singleton pattern instances that need attention
- There is only one instance of a class
- Constructor privatization
- You have to create your own instance
- Contains a static variable of this class to hold this unique instance
- Provide examples to the whole system by itself
- Provide external access to the instance object
- Direct exposure
- With static variables get Method to get
Classification of singleton patterns and several common forms
Hungry Chinese style : Create objects directly , There is no thread safety issue
Directly instantiate the starved Chinese style ( Simple and intuitive )
/* * Hungry Chinese style : * Create instance objects directly , Whether or not it is necessary to , Will create * * (1) Constructor privatization * (2) Create your own , And save... With static variables * (3) Provide instances to the outside * (4) Emphasize that this is a single case */ public class SingLeton1 { public static final SingLeton1 INSTANCE = new SingLeton1(); private SingLeton1() { } }Enumeration ( Most succinct )
/* * Enumeration type : Indicates that the type is limited * We can limit it to one , Just a single case */ public enum Singleton2 { INSTANCE }Static code block hungry han ( Suitable for complex instantiation )
/* * This method is usually used to load initialization data , Or load the data in the configuration file * * */ public class Singleton3 { public static final Singleton3 INSTANCE; private String info; static{ try { Properties pro = new Properties(); pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties")); INSTANCE = new Singleton3(pro.getProperty("info")); } catch (IOException e) { throw new RuntimeException(e); } } private Singleton3(String info){ this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Singleton3 [info=" + info + "]"; } }
Slacker type : Delay creating objects
Thread unsafe ( For single thread )
/* * Slacker type : Delay creating this instance object * (1) Constructor privatization * (2) Use static variables to save unique instances * (3) Provide static methods , Get this instance object */ public class Singleton4 { private static Singleton4 instance; private Singleton4() { } public static Singleton4 getInstance() { if(instance == null) { instance = new Singleton4(); } return instance; } }Thread safety ( For multithreading )
public class Singleton4 { private static Singleton4 instance; private Singleton4() { } public static Singleton4 getInstance() { if(instance == null) { synchronized (Singleton4.class) { if(instance == null) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } instance = new Singleton4(); } } } return instance; } }Static inner class form ( For multithreading )
/* * When the inner class is loaded and initialized , To create INSTANCE Instance object * Static inner classes do not automatically initialize with the loading and initialization of outer classes , It is to be loaded and initialized separately . * Because when the inner class is loaded and initialized , Created , So it's thread safe */ public class Singleton6 { private Singleton6(){ } private static class Inner{ private static final Singleton6 INSTANCE = new Singleton6(); } public static Singleton6 getInstance(){ return Inner.INSTANCE; } }
边栏推荐
猜你喜欢

C 字符串

Multi thread knowledge induction

Ali suggests that all POJO attributes use wrapper classes, but have you noticed these pits?

Wild pointer understanding
![[writeup]buu SQL course1[entry level]](/img/eb/1b2541b04ca231cb07f1f3706f51c7.png)
[writeup]buu SQL course1[entry level]

Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

启明智显分享| 2.8寸手持中控屏应用方案

C operator

Particle filter learning record
随机推荐
Idea pull branch code
C 数据类型
JUnit exception, a method that asserts that the exception information is not empty
C operator
Kinect2.0+ORBSLAM2_with_pointcloud_map
浏览器指纹解读
【LDA】基础知识笔记——主要是AE、VAE
【无标题】
阿裏、騰訊、拼多多垂範,產業互聯網的新邏輯漸顯
ngork实现内网穿透--免费
Self induction of exception handling
PTA:自测-3 数组元素循环右移问题 (20分)
ADSL
交换数字,异或求单,操作符相关
How to set public IP access on the H3C gr5200 router
Pta: self test -2 prime pair conjecture (20 points)
Data collection
Serialization and deserialization mechanism in terms of games
Swap numbers, XOR, operator correlation
Scala下载及IDEA安装Scala插件(保姆级教程超详细)