Last active
May 4, 2020 16:10
-
-
Save yenerm/441d338180dab984caf1e64e4a220be0 to your computer and use it in GitHub Desktop.
Kotlin singleton converted from Java
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
<!-- Copyright 2019 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
class Singleton private constructor() { | |
private var count = 0 | |
fun count(): Int { | |
return count++ | |
} | |
companion object { | |
private var INSTANCE: Singleton? = null// Double checked | |
// Single Checked | |
val instance: Singleton? | |
get() { | |
if (INSTANCE == null) { // Single Checked | |
synchronized(Singleton::class.java) { | |
if (INSTANCE == null) { // Double checked | |
INSTANCE = | |
Singleton() | |
} | |
} | |
} | |
return INSTANCE | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment