Created
March 1, 2022 08:19
-
-
Save mehdiyari/1f71c9d59f760612c424d534a6dab876 to your computer and use it in GitHub Desktop.
This gist is part of meta-programming with kotlin articles
This file contains hidden or 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
/** | |
* Simple singleton class with double-check pattern | |
*/ | |
class Singleton private constructor() { | |
private val data: String = "data property value" | |
@Volatile | |
private var singleton: Singleton? = null | |
fun getInstance(): Singleton { | |
if (singleton == null) { | |
synchronized(this) { | |
if (singleton == null) { | |
singleton = Singleton() | |
} | |
} | |
} | |
return singleton!! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment