Created
January 16, 2020 08:38
-
-
Save sudo5in5k/77c6bc399574caf1ba65278e6de346d9 to your computer and use it in GitHub Desktop.
Singletonを実装するならこう
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
class Hoge { | |
companion object { | |
@Volatile | |
private var instance: Hoge? = null | |
fun getInstance() = instance ?: synchronized(this) { | |
instance ?: Hoge().also { instance = it } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment