MVVM ViewModel 单例
如果您不需要每个屏幕的多个实例,那么将所有 viewModels 设置为单例是错误的做法吗?
Is it a wrong practice to make all viewModels singleton if you don't need multiple instances of each screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,因为单身人士是邪恶的。
您可能会遇到虚拟机保持状态的问题,该状态可能与数据库不同步并导致内存消耗过多。
由于状态被持久化,单元测试会变得更加困难。
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:
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.