当前位置:网站首页>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;
}
}
边栏推荐
- Selenium finds the contents of B or P Tags
- EN 438-7 laminated sheet products for building covering decoration - CE certification
- Clion-MinGW编译后的exe文件添加ico图标
- AITM 2-0003 水平燃烧试验
- Simple getting started example of Web Service
- 示波器探头对信号源阻抗的影响
- The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
- 校招期间 准备面试算法岗位 该怎么做?
- WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
- js常用方法封装
猜你喜欢

Teach yourself to train pytorch model to Caffe (2)

示波器探头对信号源阻抗的影响

Clion configures Visual Studio (MSVC) and JOM multi-core compilation

Add ICO icon to clion MinGW compiled EXE file

张丽俊:穿透不确定性要靠四个“不变”

使用WebAssembly在浏览器端操作Excel

CLion配置visual studio(msvc)和JOM多核编译

事项研发工作流全面优化|Erda 2.2 版本如“七”而至

【案例】元素的显示与隐藏的运用--元素遮罩

Teach yourself to train pytorch model to Caffe (III)
随机推荐
Postgres establish connection and delete records
When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
SYSTEMd resolved enable debug log
校招期间 准备面试算法岗位 该怎么做?
[daily training] 729 My schedule I
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
Dictionary tree simple introductory question (actually blue question?)
木板ISO 5660-1 热量释放速率摸底测试
Hdu2377bus pass (build more complex diagram +spfa)
leetcode:1139. The largest square bounded by 1
Comparison table of foreign lead American abbreviations
Add ICO icon to clion MinGW compiled EXE file
Sequence alignment
Learning notes of SAS programming and data mining business case 19
LeetCode: Distinct Subsequences [115]
leetcode:1755. Sum of subsequences closest to the target value
ts 之 属性的修饰符public、private、protect
poj 3414 Pots (bfs+线索)
bazel是否有学习的必要
PHP deserialization +md5 collision