“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Android architecture is a very common kind of questions in interviews. Recently, I have been sorting out a series of basic knowledge related to Android, so here I will make a simple combing and summary of the three common architectures: MVC, MVP and MVVM.

Advantages of the Android architecture

  • Flexible scalability
  • Easy to refactor
  • Easier to unit test
  • Good readability

MVC (Model-View-controller)

  • Model includes all data models and data states, and is responsible for data interaction and storage;
  • The View includes the UI presented to the user and all other interactions with the user;
  • The Controller includes business logic and the interaction between the Model and the View. It acts as a bridge between the Model and the View. Through the Controller, you can manipulate the Model and return the results to the View.

The architecture diagram of MVC architecture is as follows:

advantage

  • Models and views are decoupled, and models are easy to unit test
  • The overall architecture is simple, the development cycle is short and the speed is fast

disadvantage

  • For Android, controllers contain both views and controllers, which are difficult to extend, modify, and unit test;
  • The Model can manipulate the View directly, so when the View changes, both the Model and the Controller need to change.

MVP

  • Model includes all data models and data states, and is responsible for data interaction and storage (the same as MVC);
  • The View includes the UI presented to the user and all other interactions with the user, but it also includes a set of View interfaces that the Presenter can call;
  • A Presenter includes the business logic and the interaction between the Model and the View. In addition, it calls the set of interfaces provided by the View and communicates with the View through the interfaces to achieve decoupling.

The architecture of the MVP architecture is as follows:

advantage

  • View Interface is used to decoupage View and Presenter. In MVP architecture, Activity/Fragment only plays the role of View, so that Presenter changes do not affect View, and vice versa.
  • Model and View are decoupled at the same time. Changes in View do not affect Model and vice versa.
  • Presenter is easy to unit test and flexible to refactor, rewrite, or extend;
  • Views contain only pure UI code and are easy to reuse and switch between different views.

disadvantage

  • Would introduce so many interfaces that the project code would explode,
  • The code structure is more complex and difficult to manage and read

MVVM

  • Model includes all data models and data state, and is responsible for data interaction and storage (same as MVC and MVP).
  • The View contains the UI presented to the user and all other interactions with the user. In MVVM, XML files are used to host the UI, which can be used for DataBinding using the DataBinding framework.
  • The ViewModel includes the business logic and interaction between the Model and the View. It has the same responsibilities as a Presenter in MVP. The only difference is that the ViewModel communicates with the View through DataBinging. Using DataBinding not only greatly simplifies the amount of code, but also allows you to be aware of the component lifecycle and reduce memory leaks.

The architecture diagram of the MVVM architecture is as follows:

advantage

  • View and ViewModel are loosely coupled by DataBinding framework.
  • The ViewModel is easy to unit test because XML files do not need to be tested, so overall test coverage is very high.

disadvantage

  • For complex UI, XML file code is large and complex;
  • The overall amount of code is also large, more redundant.

Comparison & Summary

Here is a table to summarize the differences between the three architectures:

Refer to the link

Common Android Architecture

MVC vs MVP vs MVVM Architecture in Android

Android Architecture MVC, MVP, MVVM between the difference and connection (illustration + case + source code)