kotlinsingletontemplate-method-pattern

Singleton abstract class in Kotlin


I am implementing a library following a template method design pattern. It involves creating costly IO connection. To avoid any resource leaks, i want to enforce singleton instance on abstract class level. client just need to override the method which involves logic.

how can i do with kotlin?

abstract class SingletonConnection{
     fun start(){ /* code */ }
     fun connect(){ /* code */ }
     abstract fun clientLogic()   
}

If class A extends this, it should be singleton class. not allowed to initialise multiple times. how to do in kotlin?


Solution

  • Unfortunately, there is no way to enforce that only objects (singletons in Kotlin) can inherit from a certain abstract/open class or interface in Kotlin. object declaration is just syntactic sugar for a regular class with a Singleton pattern.

    I know it's not much, but I guess you can achieve this to a certain degree by adding documentation, asking users to implement this class by Singletons alone.

    By the way, I would use an interface instead of an abstract class for this purpose.