Skip to content

Instantly share code, notes, and snippets.

@prasannaboppe
Created November 28, 2018 10:07
Show Gist options
  • Save prasannaboppe/aa3e7fec6c5326a054287d383df388cd to your computer and use it in GitHub Desktop.
Save prasannaboppe/aa3e7fec6c5326a054287d383df388cd to your computer and use it in GitHub Desktop.
Singleton implementation for Kotlin (took it from Google sample)
/**
* Created by Prasanna on 28-11-2018.
*/
class KotlinSingleton {
companion object {
@Volatile
private var INSTANCE: KotlinSingleton? = null
fun getInstance(id: String): KotlinSingleton = INSTANCE ?: synchronized(this) {
INSTANCE ?: buildInstance(id).also { INSTANCE = it }
}
private fun buildInstance(id: String) = KotlinSingleton()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment