Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created October 9, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongjhih/dee9dec30947216e93ac3fe435b4d6c0 to your computer and use it in GitHub Desktop.
Save yongjhih/dee9dec30947216e93ac3fe435b4d6c0 to your computer and use it in GitHub Desktop.
open class Singleton<in T, out R>(private val creator: (T) -> R) {
@Volatile private var instance: R? = null
fun of(context: T): R {
return instance ?: synchronized(this) {
val i = instance
if (i != null) {
i
} else {
val i2 = creator(context)
instance = i2
i2
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment