Android MVVM设计模式 [英] Android MVVM Design Pattern

查看:226
本文介绍了Android MVVM设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最近发布的"Android Best Practices"一书中读到,MVVM是用于Android编程的一种很好的设计模式.在我最新的项目中亲自尝试过之后,将代码分成更易于管理的部分似乎确实有益.

I have read in the recently released 'Android Best Practices' book that a good design pattern to use for android programming is MVVM. Having tried it myself on my latest project it does seem to be beneficial in separating code into more manageable sections.

视图仅处理视图项的创建和ViewModel的接口. ViewModel实现接口并处理视图上的操作以及与Model的交互.下面的示例代码:

The View only handles creation of view items and an interface to a ViewModel. The ViewModel implements the interface and handlss operations on the view and interaction with the Model. Sample code below:

型号

 public class MyModel{
    public String myString;
    public MyModel(String myString){
       this.myString = myString;
    }
}

查看

public class MyActivity{

    public ViewManager delegate;

    public interface ViewManager{
        void registerTextView(TextView tvText);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        delegate = new ViewController(this);
        TextView tvText = (TextView) view.findViewById(R.id.tvText);
        delegate.registerTextView(tvText);
    }
}

ViewModel

ViewModel

 public class ViewController implements MyActivity.ViewManager{
     Context activity;
     TextView tvText;
     MyModel myModel;

     public ViewController(Context app_context){
        activity = app_context;
        myModel = new MyModel("Hello World");
     }

    @Override
    public registerTextView(TextView tvText){
        this.tvText = tvText;
        tvText.setText(myModel.myString);           
    }
 }

但是,我在网上其他任何地方都没有看到这种方法,并且找不到太多的信息来支持它是android的良好设计模式.我也有一些问题,例如:

However, I have not seen this approach anywhere else online and am unable to find much information that supports it being a good design pattern for android. I also have a few questions such as :

您是否应该为每个片段或活动单独拥有一个ViewModel?

Should you have a separate ViewModel for every fragment or just Activities?

这种方法在配置更改和Activity重新创建上是否具有其他类的额外开销,效果很好? 您可以将上下文转换为您的活动以启用fragmentManager的使用吗?

Does this approach perform well on configuration change and Activity recreation with the extra overhead of another class? Can you cast the context to your activity to enable use of the fragmentManager?

随着代码变得越来越复杂,这种比例如何扩展?

How does this scale as code gets more complex?

在我将所有项目都转换为MVVM之前,是否有人有使用Android设计模式的经验,或者有人可以向我介绍一些好的学习资料?

Does anyone have experience using this design pattern with android or could anyone point me in the direction of some good study material before i start converting all my projects to MVVM???

推荐答案

我将尝试发表自己的看法.我认为您提供的示例代码并没有遵循应用MVVM(或表示模型.MVVM源自表示模型)模式的核心价值.该模式的主要动机之一是使ViewModel(或Presentaion Model)成为纯POJO,以便ViewModels具有最大的可测试性.我还没有读过这本书,但是我建议您阅读Martin Fowler的有关模式的原始文章.我创建了一些示例来演示如何在Android开发中应用该模式.如果您有兴趣,可以在这里看看-相册示例,这是Martin Fowler原始专辑示例和最小演示应用程序 AndroidMVVM 的android版本.

I will try to give my opinion. I think the sample code you gave didn't follow the core value of applying MVVM(or presentation model. MVVM is originated from presentation model) pattern. One of the major motive of the pattern is to make ViewModel(or Presentaion Model) pure POJO so that ViewModels allow maxmium testability. I have not read the book, but i recommend you to read Martin Fowler's original article about the pattern. I created some examples to demonstrate how to apply the pattern in Android development. If you are interested, you can have a look here - Album Sample, which is an android translation of Martin Fowler's original album example, and AndroidMVVM, a minimal demo app.

一种应用模式的方法是:View(活动或片段+布局),ViewModel,模型(业务模型:持久层,网络等).通过这种方法,为了回答您的问题,我认为一个片段映射到一个ViewModel.

One way to apply the pattern is: View(Activity or fragment+layout), ViewModel, Model(business model: persistence layer, networking etc..). With this approach, to answer your question, i think one fragment maps to one ViewModel.

该模式是对设计的改进.如果正确应用,它将降低复杂性,而不是相反.希望这会有所帮助.

The pattern is to improve the design. When applied correctly, it will reduce the complexity not the other way around. Hope this helps.

这篇关于Android MVVM设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆