首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Java实现不同单例模式对应在Kotlin中的实现

  • 25-04-16 14:40
  • 4561
  • 13132
juejin.cn

常见的五种单例模式

  • 饿汉式
  • 懒汉式
  • 线程安全的懒汉式
  • 双重校验锁式
  • 静态内部类式
一、饿汉实现方式
Kotlin
代码解读
复制代码
//Java实现 public class SingletonDemo {     private static SingletonDemo instance=new SingletonDemo();     private SingletonDemo(){     }     public static SingletonDemo getInstance(){         return instance;     } } //Kotlin实现 object SingletonDemo
二、懒汉实现方式

Kotlin知识点:函数和属性与字段

Kotlin
代码解读
复制代码
//Java实现 public class SingletonDemo {     private static SingletonDemo instance;     private SingletonDemo(){}     public static SingletonDemo getInstance(){         if(instance == null){             instance = new SingletonDemo();         }         return instance;     } } //Kotlin实现 class SingletonDemo private constructor() {     companion object {         private var instance: SingletonDemo? = null             get() {                 if (field == null) {                     field = SingletonDemo()                 }                 return field             }         fun get(): SingletonDemo{         //这里不用getInstance作为为方法名,是因为在伴生对象声明时,内部已有getInstance方法,所以只能用其他名字          return instance!!         }     } }
三、线程安全的懒汉实现方式
Kotlin
代码解读
复制代码
//Java实现 public class SingletonDemo {     private static SingletonDemo instance;     private SingletonDemo(){} //使用同步锁     public static synchronized SingletonDemo getInstance(){         if(instance == null){             instance = new SingletonDemo();         }         return instance;     } } //Kotlin实现 class SingletonDemo private constructor() {     companion object {         private var instance: SingletonDemo? = null             get() {                 if (field == null) {                     field = SingletonDemo()                 }                 return field             } //方法声明为同步,需要添加@Synchronized注解         @Synchronized         fun get(): SingletonDemo{             return instance!!         }     } }
四、双重校验锁实现方式(Double Check)

Kotlin知识点:高阶函数和属性委托

Kotlin
代码解读
复制代码
//Java实现 public class SingletonDemo {     private volatile static SingletonDemo instance;     private SingletonDemo(){}      public static SingletonDemo getInstance(){         if(instance == null){             synchronized (SingletonDemo.class){                 if(instance == null){                     instance = new SingletonDemo();                 }             }         }         return instance;     } } //Kotlin实现 class SingletonDemo private constructor() {     companion object {         val instance: SingletonDemo by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {         SingletonDemo() }     } }
五、静态内部类实现方式
Kotlin
代码解读
复制代码
//Java实现 public class SingletonDemo {     private static class SingletonHolder{         private static SingletonDemo instance = new SingletonDemo();     }     private SingletonDemo(){         System.out.println("Singleton has loaded");     }     public static SingletonDemo getInstance(){         return SingletonHolder.instance;     } } //Kotlin实现 class SingletonDemo private constructor() {     companion object {         val instance = SingletonHolder.holder     }     private object SingletonHolder {         val holder = SingletonDemo()     } }

其他分享

  1. Android 常规基础面试题分享
  2. Kotlin日常高效编程技巧和作用域函数的使用。
  3. Kotlin 官方文档 中文版
注:本文转载自juejin.cn的QING618的文章"https://juejin.cn/post/7451802179683926068"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top