Skip to content

Instantly share code, notes, and snippets.

@mehdiyari
Created March 1, 2022 08:19
Show Gist options
  • Save mehdiyari/1f71c9d59f760612c424d534a6dab876 to your computer and use it in GitHub Desktop.
Save mehdiyari/1f71c9d59f760612c424d534a6dab876 to your computer and use it in GitHub Desktop.
This gist is part of meta-programming with kotlin articles
/**
* 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