bilal_azam
bilal_azam

Reputation: 4800

Repository Pattern Single Interface in Android

After studying a couple of blog posts on the topic, I got confused as there were different implementations by different developers but they have all used single interface for repository.

Assume, we have following interface for repository

interface Repository<T> {
    fun get(): List<T>
    fun save(items: List<T>)
}

For a specific implementation of this repository with T = Users, Repository.get(), downloads some data from sever and cache it.

For another type of object, say Bundles I not only need these methods but some additional methods e.g. querying local database for specific column.

In that case, I can't understand, do I need to create a separate interface or add methods to existing single interface, in which case what should I do with the implementation of new methods in other repositories which do not need these methods? It it must to have same interface for all repositories. Personally, I think, its ok to create multiple interfaces for different repositories as long as I abstract them nicely.

Upvotes: 0

Views: 796

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007124

do I need to create a separate interface or add methods to existing single interface

You can do whatever you like. Android does not know anything about repositories and imposes no restrictions.

In my work, I do not create a generic interface (like the one that you show) in the first place. I cannot think of a project that I worked on where developers created a generic repository interface, either. I am sure that some people do create such an interface, but there is no requirement by the OS for one.

Personally, I think, its ok to create multiple interfaces for different repositories as long as I abstract them nicely.

You are welcome to do that.

Upvotes: 1

Related Questions