Skip to content

Instantly share code, notes, and snippets.

@catalinghita8
Last active August 10, 2020 16:32
Show Gist options
  • Select an option

  • Save catalinghita8/c9b9fc71e4a671315ffb08355511c9ea to your computer and use it in GitHub Desktop.

Select an option

Save catalinghita8/c9b9fc71e4a671315ffb08355511c9ea to your computer and use it in GitHub Desktop.
interface DataSource {
fun getData(): String
}
class LocalDataClient: DataSource {
override fun getData() = sharedPreferences.getString(KEY)
}
class RemoteDataClient: DataSource {
override fun getData() = retrofit.getValue(KEY)
}
class DataViewModel(private val dataClient: DataSource) {
fun getCachedData() = dataClient.getData()
}
// Container of objects shared across the whole project
class DependencyInjector {
val localDataClient: DataSource = LocalDataClient()
val remoteDataClient: DataSource = RemoteDataClient()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment