当前位置:网站首页>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;
}
}
边栏推荐
- 示波器探头对测量带宽的影响
- 基於flask寫一個接口
- Careercup its 1.8 serial shift includes problems
- bazel是否有学习的必要
- [daily training] 729 My schedule I
- 基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
- js常用方法封装
- Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
- Aitm 2-0003 horizontal combustion test
- php中explode函数存在的陷阱
猜你喜欢
MySQL 千万数据量深分页优化, 拒绝线上故障!
【案例】元素的显示与隐藏的运用--元素遮罩
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
面试官:并发编程实战会吗?(线程控制操作详解)
Clickhouse copy paste multi line SQL statement error
第05章_存储引擎
Pytoch practice -- MNIST dataset handwritten digit recognition
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
随机推荐
hdu2377Bus Pass(构建更复杂的图+spfa)
Golang(1)|从环境准备到快速上手
Chapter 05_ Storage engine
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
AITM 2-0003 水平燃烧试验
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
What are the requirements of UL 2043 test for drive housing in the United States?
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
【案例】元素的显示与隐藏的运用--元素遮罩
Selenium finds the contents of B or P Tags
Introduction to TS, constructor and its this, inheritance, abstract class and interface
字典树简单入门题(居然是蓝题?)
ODPS 下一个map / reduce 准备
Five layer network protocol
MySQL 千万数据量深分页优化, 拒绝线上故障!
[case] Application of element display and hiding -- element mask
Aitm2-0002 12s or 60s vertical combustion test
Écrire une interface basée sur flask
Comparison table of foreign lead American abbreviations