Search code examples
androidmvvmandroid-databinding

Why to use MVVM instead of MVC in android


What I am aware of

  • I have been using MVC with android for a while
  • Now I am learning MVVM with Data binding
  • I am aware of architecture difference between the two

Questions

  • How MVVM usage is advantage to MVC
  • Is there any advantage of using ViewModel in MVVM for Automated Unit testing
  • We write more code in MVVM compared to MVC to perform similar tasks. Then what is the advantage
  • When Should I need to opt for mvc instead of mvvm & visversa

Solution

  • MVC(Model View Controller)

    I think this is the most widely used approach in Software Development. Model View Controller consist of three main components, around which the whole architecture is revolving.

    View:- This component directly interacts with user and is responsible for how user going to see our application. In MVC, Xml is treated as view.

    Model:- Model is the data Source for the Application and the main business logic is defined here, it contains data objects that are used in application and is shown to user. Data Source can be Web, local database(sqlite) etc.

    Controller:- Here comes the important part of MVC pattern, Controller is the component that manipulates, edit, uses data model and show it to users via View. Controller is responsible for gathering all data and act as middle men between model and view. Activity/Fragments are considered to be Controllers in Android. enter image description here

    MVVM (Model View View-Model)

    Model View View-Model is introduced in last year’s Google I/O.This architectural plan is becoming popular by the features it provides. It mainly implements the Data Binding Framework, it allows for “binding” of views to fields on an arbitrary object. When a field is updated, the framework is notified and the view is updated automatically. The architecture introduces two-way communication between its components. Along with features like binding, Automatically updating views, it also easy for testing purpose.The functionality of Model and View is same as we have discussed in MVP.

    View-Model :- It is responsible for exposing methods, commands, and other properties that helps to maintain the state of the view, manipulate the model as the result of actions on the view, and trigger events in the view itself.View has a reference to View-Model but View-Model has no information about the View.There is many-to-one relationship between View and View-Model means many View can be mapped to one View-Model. It is completely independent of Views.

    MVVM is the best architecture for android app development. You can understand more about it by table give below.

    enter image description here