当前位置:网站首页>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;
}
}
边栏推荐
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Mode - "Richter replacement principle"
- @Validated基础参数校验、分组参数验证和嵌套参数验证
- PostGIS installation geographic information extension
- EasyExcel的讀寫操作
- 序列联配Sequence Alignment
- systemd-resolved 开启 debug 日志
- Sophomore personal development summary
- 判断横竖屏的最佳实现
- Five layer network protocol
猜你喜欢

PVC plastic sheets BS 476-6 determination of flame propagation properties

Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development

基於flask寫一個接口

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

MySQL InnoDB Architecture Principle

请查收.NET MAUI 的最新学习资源

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

基于flask写一个接口

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

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
随机推荐
Selenium finds the contents of B or P Tags
Golang(1)|从环境准备到快速上手
Hdu2377bus pass (build more complex diagram +spfa)
systemd-resolved 开启 debug 日志
【案例】元素的显示与隐藏的运用--元素遮罩
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
Sophomore personal development summary
bazel是否有学习的必要
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
Five layer network protocol
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
五层网络协议
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
国外LEAD美国简称对照表
Comparison table of foreign lead American abbreviations
ViewRootImpl和WindowManagerService笔记
驱动壳美国测试UL 2043 符合要求有哪些?
selenium 获取dom内验证码图片
请查收.NET MAUI 的最新学习资源