如何有效地实现我的Android应用程序的MVVM设计模式,这也将简化测试用例的编写?

我正在计划为我的Android应用程序实现MVVM架构设计模式.我已经在线阅读,这将有助于我实现有效的分离问题,轻松编写数据模型,UI等测试用例.需要一些洞察/建议.
最佳答案
那么要学习如何有效地使用MVVM,从Android MVVM Design Pattern Examples开始

在这里你会发现那个帖子:

I am the developer of 07001. Like @Brentley said, it’s
a very new project but I do hope to get more buzz and experience so
that it can be improved. Back to your question, I have written some
simple introduction/tutorials on MVVM with android-binding:

  • 07002
  • 07003
  • 07004
  • 07005

Potential adopters please also register on the 07006

阅读整个主题.您会注意到MVVM是相对较新的框架,强烈建议与Google的pData Binding库和依赖注入库(如RoboguiceDagger2)配合使用…

…但最好的是这一个:

Approaching Android with MVVM. Building an MVVM architectured application using the Data Binding Library,

作者用例子中的数据绑定库解释使用MVVM – 我的意思是他自己创建的应用程序.他得出结论:

It’s still too early to know if this approach is the correct way of developing an application, but this experiment has given me a chance to look at one of the possibilities for future projects. It’s something I definitely want to play around with more.

Model-View-ViewModel很有趣,因为在传统的Android架构中,控制器会将数据推送到视图.您将在“活动”中找到该视图,然后在其上设置内容.

使用MVVM,您的ViewModel会更改某些内容,并通知绑定数据框架有关已更改的内容.框架然后自动更新任何视图,绑定到该内容.

这两个组件通过数据和命令的接口松散耦合.

使用MVVM的下一个例子真的可以测试.从MVVM on Android: What You Need to Know

Because a ViewModel does not depend on the View anymore, you can test a ViewModel without a View even existing. With proper dependency injection for other dependencies, it is very straightforward to test.

For example, instead of binding a VM to a real view, one might create a VM in a test case, give it some data, then call actions on it, to make sure the data is transformed properly. (…) All of this can be done without having to interact with an actual View.

还读:MVVM ON ANDROID USING THE DATA BINDING LIBRARY

希望有帮助

转载注明原文:如何有效地实现我的Android应用程序的MVVM设计模式,这也将简化测试用例的编写? - 代码日志