当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

PHP deserialization +md5 collision

2022-07-03-cka- latest feedback from fans

Influence of oscilloscope probe on measurement bandwidth

基于flask写一个接口

EN 438-7 laminated sheet products for building covering decoration - CE certification

Five layer network protocol

Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)

Write an interface based on flask

EasyExcel的读写操作

事项研发工作流全面优化|Erda 2.2 版本如“七”而至
随机推荐
Introduction to TS, constructor and its this, inheritance, abstract class and interface
[case] Application of element display and hiding -- element mask
Aitm 2-0003 horizontal combustion test
Get JS of the previous day (timestamp conversion)
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
Establishment of terminal security capability verification environment and penetration test records
PostGIS installation geographic information extension
序列联配Sequence Alignment
Comparison table of foreign lead American abbreviations
selenium 查找b或p标签的内容
Web Service简单入门示例
Display DIN 4102-1 Class B1 fire test requirements
Écrire une interface basée sur flask
《SAS编程和数据挖掘商业案例》学习笔记# 19
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Two ways to realize video recording based on avfoundation
木板ISO 5660-1 热量释放速率摸底测试
MYSQL IFNULL使用功能
[daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
Enclosed please find. Net Maui's latest learning resources