当前位置:网站首页>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; } }
边栏推荐
- [spark][core] what is an external shuffle service?
- 學習是一件逆人性的事情(成為高手的內功心法)
- 启明云端分享| 通过Matter协议实例演示开关通过matter协议来做到对灯亮灭的控制
- About layoffs in Internet companies
- FIRSTVT和LASTVT白话版
- 安装PS软件时提示程序无法访问关键文件/目录,错误代码:41的解决方法
- Function related matters
- Pta: self test -2 prime pair conjecture (20 points)
- TF learning notes in ROS
- 同花顺手机炒股开户安全吗
猜你喜欢

Mh32f103arpt6 hardware and software compatible alternative to stm32f103rct6

Array related content

Data collection

交换数字,异或求单,操作符相关

Structure example

Serialization and deserialization mechanism in terms of games

Selenium advanced

Pointer related concepts

Preparation of service for robot moving forward and rotating

3D reconstruction system | L3 dual view motion recovery structure (SFM binocular SFM)
随机推荐
Ali suggests that all POJO attributes use wrapper classes, but have you noticed these pits?
【LDA】LDA主题模型笔记—主要是狄利克雷
PHPstudy建站提示hosts文件可能不存在或被阻止打开,同步hosts失败怎么解决
SQL cross database injection
同花顺手机炒股开户安全吗
Error 1105: message:\“raft entry is too large
函数递归示例
Rust tip - running the tensorrt model through FFI programming
Phpstudy indicates that the hosts file may not exist or be blocked from being opened. How to resolve the failure of synchronizing hosts
Notes on ARM 64 instructions
阿里、腾讯、拼多多垂范,产业互联网的新逻辑渐显
Some useful websites
Is it safe to open an account for flush mobile stock trading
粒子滤波学习记录
Seaborn Brief
野指针理解
三维重建系统 | L3增量运动恢复结构(增量SFM)
Leetcode daily question - fair candy bar exchange
Autofac Beginner (1)
C escape character