Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVVM Design Pattern

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.

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:

Model

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

View

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

 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);           
    }
 }

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 :

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

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?

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???

like image 971
sirFunkenstine Avatar asked Jul 10 '14 14:07

sirFunkenstine


People also ask

What is MVVM pattern in Android?

MVVM stands for Model, View, ViewModel. Model: This holds the data of the application. It cannot directly talk to the View. Generally, it's recommended to expose the data to the ViewModel through Observables. View: It represents the UI of the application devoid of any Application Logic.

What is the MVVM design pattern?

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.

Is MVVM better than MVC?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.

Is MVC and MVVM same?

MVVM separates the different components of the development process into three categories, model, view and ViewModel. This typically involves code markup or graphical user interfaces (GUI). MVC, or model-view-control is a way developers separate programs into these three components.


2 Answers

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.

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.

like image 50
Cheng Avatar answered Oct 21 '22 04:10

Cheng


Android MVVM Design Pattern

enter image description here

The Data Binding Library offers both flexibility and broad compatibility — it's a support library, so you can use it with all Android platform versions back to Android 2.1

Build Environment

android {
    ....
    dataBinding {
        enabled = true
    }
}

You can follow this link step by step and apply databinding in your android projects.

Advance Guide go to developer page Link

like image 5
Ramkailash Avatar answered Oct 21 '22 02:10

Ramkailash