Last active
November 11, 2019 09:02
-
-
Save mezentsev/e52c99fe121b2c235dad9edad0c3b3e9 to your computer and use it in GitHub Desktop.
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 MyMagicClass private constructor(builder: Builder) { | |
class Builder { | |
} | |
companion object { | |
@Volatile | |
private var instance: MyMagicClass? = null | |
fun init(builder: Builder) { | |
if (instance != null) { | |
throw IllegalStateException("Already init") | |
} | |
synchronized(this) { | |
if (instance == null) { | |
instance = MyMagicClass(builder) | |
} else { | |
throw IllegalStateException("Already init") | |
} | |
} | |
} | |
fun instance() = instance ?: throw IllegalStateException("Not init yet") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment