In this sample app: https://github.com/googlesamples/android-sunflower
The repository classes (PlantRepository.kt, GardenPlantingRepository.kt) are singletons and so is the injector utility object (InjectorUtils.kt).
Why are the repository classes singletons when the injector utility object is already one?
InjectorUtils is a singleton, because they want to use it as an helper class from anywhere in the code in a static way, and through it been able to get a PlantRepository, a GardenPlantingRepository, and some factories. Now, if you think about Repositories, they are classes to provide fast access to in-memory datasets (which can be then updated asynchronously), so there wouldn't be any reason to create any time a new instance of a repository (that means any time new memory will be allocated), given its methods are only wrappers around the dao's methods; in fact, if you notice, the dao is passed to such singleton repositories in getInstance(), therefore allowing the binding between the wrapper methods and the dao. So, they are singletons to avoid allocating new memory, passing instead around the same allocated memory (because it contains only wrappers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With