废话不多说,直接上代码,记录一下,方便以后使用
class ImageClassifyUtil private constructor(){
/**
* 饿汉
*/
companion object {
val instance = ImageClassifyUtil()
}
/**
* 懒汉
*/
companion object {
private var instance: ImageClassifyUtil? = null
get() {
if (field == null) {
field = ImageClassifyUtil()
}
return field
}
fun get(): ImageClassifyUtil {
return instance!!
}
}
/**
* 线程安全的懒汉
*/
companion object {
private var instance: ImageClassifyUtil? = null
get() {
if (field == null) {
field = ImageClassifyUtil()
}
return field
}
@Synchronized
fun get(): ImageClassifyUtil {
return instance!!
}
}
/**
* 双重校验
*/
companion object {
val instance: ImageClassifyUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
ImageClassifyUtil()
}
}
/**
* 静态内部类
*/
companion object {
val instance = ImageClassifyHolder.holder
}
private object ImageClassifyHolder {
val holder = ImageClassifyUtil()
}
}
ps:历史给人的唯一教训,就是人们从未在历史中吸取过任何教训
ps:秦人不暇自哀,而后人哀之;后人哀之而不鉴之,亦使后人而复哀后人也”基本同义。