MVVM ViewModels Singleton

2019-03-24 20:08发布

Is it a wrong practice to make all viewModels singleton if you don't need multiple instances of each screen?

3条回答
爷、活的狠高调
2楼-- · 2019-03-24 20:54

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-24 20:56

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.

查看更多
祖国的老花朵
4楼-- · 2019-03-24 21:13

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.

查看更多
登录 后发表回答