代码
package com.flannery.interviewdemo.singleinstance
object SingleInstance1
class SingletonDemo2 private constructor() {
companion object {
private var instance: SingletonDemo2? = null
get() {
if (field == null) {
field = SingletonDemo2()
}
return field
}
fun get(): SingletonDemo2 {
return instance!!
}
}
}
class SingletonDemo3 private constructor() {
companion object {
private var instance: SingletonDemo3? = null
get() {
if (field == null) {
field = SingletonDemo3()
}
return field
}
@Synchronized
fun get(): SingletonDemo3 {
return instance!!
}
}
}
class SingletonDemo4 private constructor() {
companion object {
val instance: SingletonDemo4 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
SingletonDemo4()
}
}
}
class SingletonDemo5 private constructor() {
companion object {
val instance = SingletonHolder.holder
}
private object SingletonHolder {
val holder= SingletonDemo5()
}
}
class SingletonDemo6 private constructor(private val property: Int) {
companion object {
@Volatile private var instance: SingletonDemo6? = null
fun getInstance(property: Int) =
instance ?: synchronized(this) {
instance ?: SingletonDemo6(property).also { instance = it }
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
java实现的双重校验反编译成smali
.class public LA001单例模式/DoubleCheckSingleInstance;
.super Ljava/lang/Object;
.source "DoubleCheckSingleInstance.java"
# static fields
.field private static volatile instance:LA001单例模式/DoubleCheckSingleInstance;
# direct methods
.method static constructor ()V
.registers 1
.prologue
.line 4
const/4 v0, 0x0
sput-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
return-void
.end method
.method public constructor ()V
.registers 1
.prologue
.line 3
invoke-direct {p0}, Ljava/lang/Object;->()V
return-void
.end method
.method public static getInstance()LA001单例模式/DoubleCheckSingleInstance;
.registers 2
.prologue
.line 7
sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
if-nez v0, :cond_13
.line 8
const-class v1, LA001单例模式/DoubleCheckSingleInstance;
monitor-enter v1
.line 9
:try_start_7
sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
if-nez v0, :cond_12
.line 10
new-instance v0, LA001单例模式/DoubleCheckSingleInstance;
invoke-direct {v0}, LA001单例模式/DoubleCheckSingleInstance;->()V
sput-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
.line 12
:cond_12
monitor-exit v1
:try_end_13
.catchall {:try_start_7 .. :try_end_13} :catchall_16
.line 14
:cond_13
sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
return-object v0
.line 12
:catchall_16
move-exception v0
:try_start_17
monitor-exit v1
:try_end_18
.catchall {:try_start_17 .. :try_end_18} :catchall_16
throw v0
.end method
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
资料
Kotlin的5种单例模式