import kotlin.reflect.KClass interface Singleton interface IView : Singleton interface IFragment interface IUIComponent abstract class UIComponent : IUIComponent abstract class View : IView, UIComponent() abstract class Fragment : IFragment, UIComponent() val injectionContext = mutableMapOf, Singleton>() fun findSingleton(type: KClass): T { if (!injectionContext.containsKey(type)) injectionContext[type] = type.java.newInstance() return injectionContext[type] as T } fun findFragment(type: KClass): T = type.java.newInstance() inline fun findUIComponent(type: KClass): T where T : IView, T : IFragment { if (T::class is IView) return findSingleton(type) if (T::class is IFragment) return findFragment(type) throw RuntimeException("Unknown UIComponent sub type") }