Skip to content

Instantly share code, notes, and snippets.

@Garyfimo
Created July 5, 2020 21:54
Show Gist options
  • Save Garyfimo/56d348035df3dc4459ba142635522e4c to your computer and use it in GitHub Desktop.
Save Garyfimo/56d348035df3dc4459ba142635522e4c to your computer and use it in GitHub Desktop.
class AppServiceLocator : ServiceLocator {
private val calculadorUseCase: CalculadorUseCase by lazy {
CalculadorUseCaseImpl()
}
private val evaluadorUseCase: EvaluadorUseCase by lazy {
EvaluadorUseCaseImpl(validadorUseCase, evaluadorExpresionUseCase)
}
private val evaluadorExpresionUseCase: EvaluadorExpresionUseCase by lazy {
EvaluadorExpresionUseCaseImpl(calculadorUseCase)
}
private val validadorUseCase: ValidadorUseCase by lazy {
ValidadorUseCaseImpl()
}
override val mainViewModelFactory: MainViewModelFactory by lazy {
MainViewModelFactory(evaluadorUseCase)
}
}
class CalculadoraApplication : Application() , ServiceLocatorProvider {
override val serviceLocator: ServiceLocator by lazy { AppServiceLocator() }
}
interface ServiceLocator {
val mainViewModelFactory : MainViewModelFactory
}
interface ServiceLocatorActivity : ServiceLocatorProvider {
val self: Activity
override val serviceLocator: ServiceLocator
get() = (self.application as? ServiceLocatorProvider)?.serviceLocator
?: throw IllegalStateException("Application must implement ServiceLocatorProvider in order to provide global service locator, or implement this property to provide your own ServiceLocator")
}
interface ServiceLocatorProvider {
val serviceLocator: ServiceLocator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment