Is it a wrong practice to make all viewModels singleton if you don't need multiple instances of each screen?
问题:
回答1:
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.
回答2:
singletons:
- make testing harder
- give you probolems later if you do need more then 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 - "becouse you can" is not a good reasion.
回答3:
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.