If you have many such resettable singletons, you c...
# announcements
o
If you have many such resettable singletons, you can reuse the code:
Copy code
class Singleton {
    companion object : SingletonCompanion<Singleton>({ Singleton() })
}
class Singleton2 {
    companion object : SingletonCompanion<Singleton2>({ Singleton2() })
}

open class SingletonCompanion<T>(val constructor: () -> T) {
    private var _instance: T? = null
    fun getInstance() = _instance ?: constructor().apply { _instance = this }
    fun resetInstance() {
        _instance = null
    }
}