Sobes.tech
Junior — Middle

Why is it important to create interfaces for remote data sources in the system?

sobes.tech AI

Answer from AI

Creating interfaces for remote data sources is important for several reasons:

  • Abstraction and isolation: The interface hides implementation details, allowing changes to the data source (e.g., from REST API to GraphQL or local database) without altering the rest of the code.
  • Testability: Mocks can be easily substituted through the interface for unit tests, simplifying the verification of business logic without real network connections.
  • Maintainability: A clearly defined interaction contract makes the code easier to understand and support.
  • Flexibility: It allows implementing different data retrieval options (cache, network, database) with the same interface.

Example of an interface in Kotlin for a remote data source:

interface RemoteDataSource {
    suspend fun fetchData(): List<DataItem>
}

class ApiRemoteDataSource(private val apiService: ApiService) : RemoteDataSource {
    override suspend fun fetchData(): List<DataItem> {
        return apiService.getData()
    }
}