Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the repository classes singletons in this sample app?

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?

like image 651
oznecro Avatar asked Oct 28 '22 06:10

oznecro


1 Answers

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)

like image 59
Alessio Avatar answered Nov 12 '22 21:11

Alessio