https://kotlinlang.org logo
#announcements
Title
# announcements
d

doodla

10/07/2019, 5:52 AM
What’s the kotlin way to do singletons? I know about `object`s but they’re not easy to test, or inject dependencies into ( unless i’m misunderstanding something ).
d

Derek Peirce

10/07/2019, 6:48 AM
The "Kotlin way" would be
object
. If you want the singleton to have injected dependencies, then you'll probably want to use a dependency injection framework like Dagger2.
y

yawkat

10/07/2019, 8:14 AM
to be specific: If you want DI, you do singletons exactly like you would in java.
g

gildor

10/07/2019, 11:53 AM
If you want DI than you don't need Singletons, like in Java or like
object
Not easy to test or if you need DI means that you overuse Singletons for classes that should be just ordinary class, injected as dependency to another class and amount of instances regulated by your DI graph
3 Views