Last active
October 25, 2020 12:30
-
-
Save skywall/508c1bac9a015de3b6c3aeed77709207 to your computer and use it in GitHub Desktop.
SingletonHolder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class SingletonHolder<out T, in A>(private val constructor: (A) -> T) { | |
@Volatile private var instance: T? = null | |
fun getInstance(arg: A): T { | |
return when { | |
instance != null -> instance!! | |
else -> synchronized(this) { | |
if (instance == null) { | |
instance = constructor(arg) | |
} | |
instance!! | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment