Created
October 9, 2019 20:45
-
-
Save yongjhih/dee9dec30947216e93ac3fe435b4d6c0 to your computer and use it in GitHub Desktop.
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 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