First, let’s introduce the structure of this paper:

  1. Concrete concepts of the three architectures
  2. Writing in android development, advantages and disadvantages

1. The concept

1.1 the MVC

MVC pattern (Model — View — Controller) is a software architecture pattern in software engineering. It divides the software system into three basic parts: Model, view and controller, so as to facilitate the hierarchical development of programs.

The MVC pattern, first proposed by Trygve Reenskaug in 1978, is a software architecture invented by Xerox PARC in 1980s for Smalltalk, a programming language. — Wikipedia

  • Model – Defines the data Model that the user interface needs to display and the data Model that contains the associated business logic.
  • View – A terminal interface that is presented to the user, primarily for visualization of data in the model.
  • Controller – Acts on models and views. It controls the flow of data to model objects and updates the view as the data changes. It separates the view from the model.

The two dotted lines in the figure actually correspond to MVC pattern 2 in the Android development process, which will be described in detail next.

1.2 the MVP

MVP pattern (Model-view-Presenter) is a variation of MVC pattern. In MVP mode, to separate the UI layer from the logical layer, a Presenter layer is added between the UI layer and the logical layer. The interaction between presenters and them is usually specified as an interface. Respect this contract, design and develop according to it, whether it’s UI development or data logic development.

  • Model – Defines the data Model that the user interface needs to display and the data Model that contains the associated business logic.
  • View – The terminal interface presented to the user, mainly used to visualize data in the model.
  • Presenter – An event handler that contains a component and is responsible for retrieving data from the Model and communicating it to the View through formatting.

1.3 MVVM

MVVM pattern (Model — view — ViewModel) is also based on MVC evolution, it is mainly based on data binding framework to optimize MVC. MVVM is also known as Model-View-Binder, as are ZK (a Web application framework written in Java) and KnockoutJS (a JavaScript library).

MVVM was developed by Microsoft architects Ken Cooper and Ted Peters by leveraging WPF (Microsoft). NET graphics system) and Silverlight (an Internet application derivative of WPF) to simplify event-driven programming for user interfaces. John Gossman, one of Microsoft’s WPF and Silverlight architects, published MVVM on his blog in 2005.

Model and View are basically the same as above. ViewModel serves as a bridge between them. Compared with MVP mode, its ViewModel can be regarded as a combination of a Model and Presenter of a View.

Bind ViewModel and View with data binding framework:

  • When the View changes, it automatically updates to the ViewModel.
  • When the ViewModel changes, the View is automatically updated.

2. Writing methods, advantages and disadvantages

MVC

  • The Model layer in Android is usually our bean class and data fetch stuff.
  • The View layer corresponds to our layout. XML layout file and Activity, mainly because of the weak function of Layout. XML in Android, most View operations also need to be handled by Activity.
  • A Controller corresponds to our Activity.

In addition, there are two main ways to write MVC in Android:

  1. There is no direct interaction between the Model layer and the View layer. After data changes, the C layer is notified first, and the C layer completes the View refresh.Corresponds to <------ in the MVC diagram
  2. The Model layer can directly notify the View of updates.Corresponding to <····· in MVC diagram

Corresponding disadvantages:

  1. The first one is because the layout. XML function in Android is too weak, most view operations also need to be handled by the Activity. This results in an overly bloated Activity code.
  2. The second version takes the burden off the Activity a bit, but it results in the Model and View layers being too tight and coupled.
  3. Both make code difficult to maintain and difficult to test.

Advantages: Easy to write, relatively simple structure.


MVP

  • M: Again, data capture and entity model
  • V: Corresponding to XML and Activity, responsible for View drawing and user interaction
  • P: Responsible for completing the interaction between View and Model, write some business logic processing here, and realize it through interface definition.

Advantages:

MVP mode lightened the burden of activities, separated the business logic from the view, reduced coupling, easy to reuse code and easy to test a single module.

Disadvantages:

  • Increased code complexity, for some simple modules, will make the program redundant.
  • In the MVP architecture, Presenter is not aware of the Activity and Fragment life cycle, so we need to manually add the corresponding life cycle methods and special handling to avoid exceptions or memory leaks

The MVVM:

Lifecycle*** is the core of the MVVM architecture. On top of this, Google has developed DataBinding, ViewModel, and LiveData to implement the full MVVM architecture.

Advantages:

  • Unit testing convenience
  • After binding, you can reduce the template code, callback interface writing, the code is more concise

Disadvantages:

  • Classes will grow and reference packages will be added.
  • Two-way data binding is not conducive to code reuse. The most common reuse in client development is views, but two-way data binding allows you to bind a Model to each View, and the model varies from module to module. You can’t simply reuse views.
  • The model in a large module will also be very large, which will cost more memory if it is held for a long time.

3 model instance of the code may refer to: mp.weixin.qq.com/s/gF8IxGE8B…