当前位置:网站首页>Simple interest mode - evil Chinese style
Simple interest mode - evil Chinese style
2022-07-05 21:15:00 【Try to survive】
/* * Singleton design pattern ( a key ) * * Single case : Unique instance , Throughout Java In the system , There is only one object of a class . * * form : * 1、 hungry ( evil ) Chinese style * Whether the user wants to use this object or not , Create this object first . * 2、 Slacker type * Only when the user gets this object , To create this object . * * No matter what kind of singleton design pattern is written : * (1) The constructor of this class is Privatization --> Make sure our users don't create the second object at will ... * (2) This unique instance must be created in this class , And use a static variable to store . * * */
public class TestSingleton {
}
/* * One 、 Hungry Chinese style * 1、 Use one ( Public static constants ) To save this unique instance object * 2、 Enumeration class * 3、 Use one ( Private static constants ) To save this unique instance object , Use a static method to return this unique object */
public class TestHungry {
public static void main(String[] args) {
// Hungry h = new Hungry();// Cannot create object outside
// How to get the object of this singleton
Hungry h = Hungry.INSTANCE;
HungryEnum he = HungryEnum.INSTANCE;
HungryDemo hd = HungryDemo.getInstance();
}
}
class Hungry{
//(2) Use a static variable , To store this unique object
// This object is created when the class is initialized
public static final Hungry INSTANCE = new Hungry();
enum HungryEnum{
INSTANCE
}
class HungryDemo{
//(2) Use a static variable , To store this unique object
// This object is created when the class is initialized
private static final HungryDemo INSTANCE = new HungryDemo();
//(1) Constructor privatization
private HungryDemo(){
}
public static HungryDemo getInstance(){
return INSTANCE;
}
}
边栏推荐
- bazel是否有学习的必要
- Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
- How to send samples when applying for BS 476-7 display? Is it the same as the display??
- shell编程100例
- WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
- MYSQL IFNULL使用功能
- Binary search
- EN 438-7 laminated sheet products for building covering decoration - CE certification
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Reading and writing operations of easyexcel
猜你喜欢
示波器探头对测量带宽的影响
Five layer network protocol
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
2022-07-03-CKA-粉丝反馈最新情况
Teach yourself to train pytorch model to Caffe (I)
ArcGIS栅格重采样方法介绍
Influence of oscilloscope probe on signal source impedance
XML建模
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
随机推荐
Postgres establish connection and delete records
Chapter 05_ Storage engine
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
R language [data management]
Sequence alignment
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
PHP deserialization +md5 collision
100 cases of shell programming
shell编程100例
Golang(1)|从环境准备到快速上手
XML modeling
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
ViewRootImpl和WindowManagerService笔记
使用WebAssembly在浏览器端操作Excel
[case] Application of element display and hiding -- element mask
国外LEAD美国简称对照表
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Which securities company is better and which platform is safer for stock account opening