SingletonHolder
A base class for singletons that need one construction argument.
This is the multiplatform companion-object pattern for a singleton with an argument: the companion object of the singleton class extends this holder and callers obtain the instance through getInstance.
class Analytics private constructor(config: Config) {
companion object : SingletonHolder<Analytics, Config>(::Analytics)
}
val analytics = Analytics.getInstance(config)Content copied to clipboard
The creator function runs on the first getInstance call and its reference is released afterwards; every later call returns the same instance and ignores its argument.
This class is not thread-safe.
Type Parameters
T
the singleton type.
A
the construction argument type.