sunflower icon indicating copy to clipboard operation
sunflower copied to clipboard

Rather than converting repository classes to Singletons, they should be objects.

Open shalomhalbert opened this issue 7 years ago • 3 comments

shalomhalbert avatar Oct 31 '18 16:10 shalomhalbert

Totally agree. Just wonder why still need these things on Kotlin

    companion object {

        // For Singleton instantiation
        @Volatile private var instance: GardenPlantingRepository? = null

        fun getInstance(gardenPlantingDao: GardenPlantingDao) =
                instance ?: synchronized(this) {
                    instance ?: GardenPlantingRepository(gardenPlantingDao).also { instance = it }
                }
    }

namchuai avatar Oct 31 '18 16:10 namchuai

I had the same question before, however, after some sleeping and thinking I find that the object class hasn't chance to consume parameters, I think this is the only reason to use this style, when there isn't DI plugin in the project.

XinyueZ avatar Nov 05 '18 19:11 XinyueZ

This is the kind of code that makes your average developer's head spin and is what has been driving complaints about Android development being hard to get right. Really jetpack project goal is to make it easier to write android code and not force developers to write or understand these kinds of things. If sunflower project contains code like this then clearly jetpack project isn't really complete!

Double checked locking is a cool performance trick but it should be an internal implementation detail that your average dev need not understand or write by hand over and over again.

satur9nine avatar Aug 11 '19 18:08 satur9nine