不可观察的模型对象是否违反了Android中的MVVM架构中的ViewModel?

huangapple go评论78阅读模式
英文:

Is non-observable model object in the ViewModel against MVVM architecture in Android?

问题

我在我的片段的“ViewModel”中有一个名为Category的不可观察模型类,并且我的片段(作为“View”)直接访问它。这个模型不应该在以后被更改,我将其声明为一个最终属性。现在我的问题是,通过在“ViewModel”内部声明一个不可观察的模型,我是否违反了“MVVM架构”?
谢谢

英文:

I have a non-observable model class called Category in my fragment's ViewModel and my fragment(as View) is accessing it directly. This model is not supposed to be changed later and I declared it as a final property. Now my question is am I violating MVVM Architecture by declaring a non-observable model inside ViewModel?
Thanks

答案1

得分: 1

MVVM架构旨在使您的逻辑和数据具有响应性,这意味着在与应用程序交互后,用户应该看到UI、数据输出等的变化。所有呈现给用户的更改都应由观察者处理,它们从ViewModels中观察可变数据,并通过更改视觉数据来“反应”突变。

如果您的模型不发生变化,则无需观察它,因为不会有任何变化来触发观察者。

英文:

MVVM Architecture is aimed towards making your logic and data reactive, that means that after interacting with the application user should see changing of UI, data output and etc. All the changes presented to user should be handled by observers that observe mutable data from ViewModels and "react" to mutations by changing visual data.

If your model does not change, there is no need to observe it, since there will be no change to trigger the observer.

huangapple
  • 本文由 发表于 2020年9月11日 17:18:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63844171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定