MVVM ViewModels Singleton
I开发者_JAVA技巧s it a wrong practice to make all viewModels singleton if you don't need multiple instances of each screen?
Yes because singletons are evil.
You will probably run into issues where the VMs are holding onto state which could be out of sync with your database and lead to excessive memory consumption. It will be much harder to unit test due to the state being persisted.
Singletons:
- make testing harder
- give you problems later if you do need more than one of them
- are hard to control where they are created
So only use the singleton pattern if you have a very good reason to do so - "because you can" is not a good reason.
Yes.
First, you may very well be putting yourself in a corner for any extensibility, depending on the singleton implementation. Second, the design probably won't be very clean referring to static singletons everywhere. Third, unit testing will either be difficult or it won't replicate actual class usage, or both. Forth, do having singletons solve any design problems for you? If you are simply trying to save on resources then I would just forget it.
精彩评论