class ServiceLocatorApplication : Application() { /** * Dependency injection using service locator pattern */ override fun getSystemService(name: String?): Any { // All this logic can be extracted into a separate "Module" class return when (name) { // We use the full class' name as the key PlacesPresenter::class.java.name -> PlacesPresenter(getSystemService(), getSystemService()) ILocationRepository::class.java.name -> GmsLocationRepository(getSystemService()) IPlacesRepository::class.java.name -> GmsPlacesRepository(getSystemService()) FusedLocationProviderClient::class.java.name -> LocationServices.getFusedLocationProviderClient(this) GeoDataClient::class.java.name -> Places.getGeoDataClient(this, null) else -> super.getSystemService(name) } } } /** * Generic strongly-typed extension method for injection */ inline fun Application.getSystemService() : T { // We extract the class name from the generic type return this.getSystemService(T::class.java.name) as T }