当前位置:网站首页>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;
}
}
边栏推荐
- CareerCup它1.8 串移包括问题
- 【案例】定位的运用-淘宝轮播图
- leetcode:1755. Sum of subsequences closest to the target value
- LeetCode: Distinct Subsequences [115]
- 2022-07-03-CKA-粉丝反馈最新情况
- Which securities company is better and which platform is safer for stock account opening
- Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
- 终端安全能力验证环境搭建和渗透测试记录
- R语言【数据管理】
- MySQL InnoDB Architecture Principle
猜你喜欢
【案例】元素的显示与隐藏的运用--元素遮罩
Wood board ISO 5660-1 heat release rate mapping test
Longest swing sequence [greedy practice]
EasyExcel的读写操作
使用WebAssembly在浏览器端操作Excel
Clickhouse copy paste multi line SQL statement error
Teach yourself to train pytorch model to Caffe (III)
XML modeling
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
Talk about my fate with some programming languages
随机推荐
面试官:并发编程实战会吗?(线程控制操作详解)
显示屏DIN 4102-1 Class B1防火测试要求
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
[daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
PVC 塑料片BS 476-6 火焰传播性能测定
PHP deserialization +md5 collision
js常用方法封装
php中explode函数存在的陷阱
AITM2-0002 12s或60s垂直燃烧试验
Pytorch实战——MNIST数据集手写数字识别
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
CLion配置visual studio(msvc)和JOM多核编译
What are the requirements of UL 2043 test for drive housing in the United States?
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
秋招将临 如何准备算法面试、回答算法面试题
123456
Learning notes of SAS programming and data mining business case 19
Is it necessary for bazel to learn