Slackbot
04/01/2018, 9:46 AMkarelpeeters
04/01/2018, 10:45 AMlouiscad
04/01/2018, 11:04 AMjossiwolf
04/01/2018, 11:05 AMsingleton class ViewModelFactory(dependency: Something)
Edit: Of course it could just be an object
that accepts argumentsAndreas Sinz
04/01/2018, 11:32 AMgildor
04/01/2018, 11:46 AMjossiwolf
04/01/2018, 11:49 AMval factory = ViewModelFactory(...)
)
Two methods could be used for retrieval of the instance: One getInstance(parameters)
and one getInstance throws NotInitializedException
without any parameters
But I'm sure there could be other ways
I thought about that too @gildor, but I feel like DI shouldn't be the only way to do that as it can get quite complex and isn't exactly easy to get started withgildor
04/01/2018, 11:52 AMgildor
04/01/2018, 11:52 AMgildor
04/01/2018, 11:53 AMtschuchort
04/01/2018, 7:18 PMViewModelProvider.Factory
is there even a reason it has to be a singleton? Those things should have no mutable state so the instances are basically unary functions and instantiating corresponds to partial application of an n-ary function. So as long as the parameters are the same, the functions are equivalent. (What should happen if a parameterized singleton is instantiated with different arguments? You can either throw, ignore it or create a different one, in which case it's no longer a singleton).
IMO a true-to-its-name singleton is usually an anti-pattern anyway. As you realized, even if the dependencies are available globally it often still makes sense to inject them, for example to make the class testable. For the sake of our sanity the object's lifecycle should be bound to some other object (probably the DaggerAppComponent). Make lifecycle explicit and pull out that responsibility. The class has no god damn business knowing that it will be used as a singleton.jossiwolf
04/02/2018, 8:51 AM