nickbutcher / plaid

An Android app which provides design news & inspiration as well as being an example of implementing material design.
Apache License 2.0
16.26k stars 3.16k forks source link

Using singleton design pattern for repositories #742

Open mahdiyar-oraei opened 5 years ago

mahdiyar-oraei commented 5 years ago

Why are you using singleton design pattern and @FeatureScope together to create instances of repositories instead only marking them as @FeatureScope?

companion object {
        @Volatile
        private var INSTANCE: ShotsRepository? = null

        fun getInstance(remoteDataSource: SearchRemoteDataSource): ShotsRepository {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: ShotsRepository(remoteDataSource)
                    .also { INSTANCE = it }
            }
        }
}
    @Provides
    @FeatureScope
    fun provideShotsRepository(remoteDataSource: SearchRemoteDataSource) =
        ShotsRepository.getInstance(remoteDataSource)
guelo commented 5 years ago

Good observation. I don't understand the use of FeatureScope at all since the components are created in the inject methods which are called from each Activity's onCreate, which means they are discarded on rotation. Since injections will only occur once what is the point of scopes?