带接口的Android存储库实现

通常,我在存储库中使用接口:

interface UserRepository {
    fun add()
    fun get(userId): User
    .....
}

我有三种类型的存储库(在内存,数据库和网络中):

They all implement UserRepository: UserInMemoryRepository - UserDatabaseRepository - UserNetworkRepository

但是我的网络没有任何添加用户API

最佳做法是什么?

  1. 删除我的UserRepository界面
  2. 保留一个空的添加用户方法
  3. ......(任何更好的方法)
评论
  • lest
    lest 回复

    Leaving add() empty doesn't goes in pair with SOLID principles. If more than one class should override some method, then you shouldn't remove interface either. What I would do is create interface:

    interface Repository {
        fun get(userId): User
    }
    ...
    interface UserRepository : Repository {
         fun add()
    }
    

    Then your "Network" can implement Repository, and the rest of classes implement UserRepository. Of course naming of interfaces is up to you, but according to 'I' in SOLID it is better to have more dedicated interfaces rather than one multifunctional