当前位置:网站首页>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;
}
}
边栏推荐
- Postgres establish connection and delete records
- 股票开户选择哪家证券公司比较好哪家平台更安全
- systemd-resolved 开启 debug 日志
- PostGIS installation geographic information extension
- XML建模
- Cross end solution to improve development efficiency rapidly
- Selenium's method of getting attribute values in DOM
- Comparison table of foreign lead American abbreviations
- MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
- Realize the function of verifying whether the user has completed login when browsing the page
猜你喜欢
Golang (1) | from environmental preparation to quick start
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
秋招将临 如何准备算法面试、回答算法面试题
EasyExcel的讀寫操作
Learning robots have no way to start? Let me show you the current hot research directions of robots
Teach yourself to train pytorch model to Caffe (I)
2022-07-03-cka- latest feedback from fans
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
EasyExcel的读写操作
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
随机推荐
[case] Application of element display and hiding -- element mask
判断横竖屏的最佳实现
秋招将临 如何准备算法面试、回答算法面试题
ODPs next map / reduce preparation
Clickhouse copy paste multi line SQL statement error
EasyExcel的读写操作
Generics of TS
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
Learning robots have no way to start? Let me show you the current hot research directions of robots
Pytorch实战——MNIST数据集手写数字识别
What are the requirements of UL 2043 test for drive housing in the United States?
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
Dictionary tree simple introductory question (actually blue question?)
Opérations de lecture et d'écriture pour easyexcel
CLion配置visual studio(msvc)和JOM多核编译
Teach yourself to train pytorch model to Caffe (III)
shell编程100例
Enclosed please find. Net Maui's latest learning resources
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
Clion configures Visual Studio (MSVC) and JOM multi-core compilation