当前位置:网站首页>直播平台开发,直播各个分类单例设计展示
直播平台开发,直播各个分类单例设计展示
2022-06-21 16:08:00 【云豹网络科技】
直播平台开发,直播各个分类单例设计展示
一、 饿汉式实现
立即加载就是使用类的时候已经将对象创建完毕(不管以后会不会使用到该实例化对象,先创建了再说。很着急的样子,故又被称为“饿汉模式”),常见的实现办法就是直接new实例化。
public class Singleton {
/** * 将自身实例化对象设置为一个属性,用static、final修饰 */
private static final Singleton INSTANCE = new Singleton();
/** * 构造方法私有化 */
private Singleton(){
}
/** * 静态方法返回实例 * @return */
public static Singleton getInstance()
{
return INSTANCE;
}
}
二、懒汉式实现
延迟加载就是调用get()方法时实例才被创建(先不急着实例化出对象,等要用的时候才给你创建出来。不着急,故又称为“懒汉模式”),常见的实现方法就是在get方法中进行new实例化。
注意: 以下代码在多线程环境中是完全错误的,根本不能保证单例的状态
public class Singleton2 {
private static Singleton2 instance;
private Singleton2(){
}
public static Singleton2 getInstance()
{
if(instance == null){
instance = new Singleton2();
}
return instance;
}
}
内部类实现单例
这种方式没有线程问题,只要不调用getInstance()方法,就不会使用内部类,内部类一旦被使用只会被初始化一次,以后一直用的就是静态常量 INSTANCE 了。
public class Singleton3 {
private Singleton3(){
}
public static Singleton3 getInstance()
{
return SingletonHodler.INSTANCE;
}
/** * 静态内部类 */
private static class SingletonHodler
{
private static final Singleton3 INSTANCE = new Singleton3();
}
}
以上就是直播平台开发,直播各个分类单例设计展示, 更多内容欢迎关注之后的文章
边栏推荐
- 3DE 运动轮廓数据修改
- AttributeError: ‘Book‘ object has no attribute ‘sheet‘
- node服务器 res.end()中写中文,客户端中乱码问题的解决方法
- Clickhouse learning notes 2: basic use tutorial
- vector的模拟实现
- Why is rediscluster designed with 16384 slots?
- Compose 中的附带效应
- 软件测试体系学习及构建(13)-测试基础之测试工程师的基本要求
- My gadget - card learning app is complete
- 窗帘做EN 1101易燃性测试过程是怎么样的?
猜你喜欢

软件测试体系学习及构建(14)-测试基础之软件测试和开发模型概述

Analysis of 43 cases of MATLAB neural network: Chapter 26 classification of LVQ Neural Network - breast tumor diagnosis

3DE 运动轮廓数据修改

How to adjust 3DE 3D model view if you can't see it

类、接口、函数

加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证

《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别

Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"

3de 3D model View ne voit pas comment ajuster

Common setting modes
随机推荐
Convert longitude and latitude to distance
Uni app framework learning notes
Side effects in compose
PTA L3-031 千手观音 (30 分)
第五章 操作位和位串
Class, interface, function
Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
《MATLAB 神经网络43个案例分析》:第26章 LVQ神经网络的分类——乳腺肿瘤诊断
Vscade tool
常见设置模式
Android kotlin class delegation by, by lazy key
Is it safe and reliable to open futures accounts on koufu.com?
Chapter V operation bit and bit string
3DE 网格坐标点与物体的附加
BM23 二叉树的前序遍历
程序员进修之路
Kotlin常用函数 let,with,apply,also,run
Nacos registry ----- built and used from 0
Redis6.0新特性(上)