MVP is a derivative of MVC

MVC (Model-View-controller)

Three parts of the MVC pattern:

  • Model includes all data models and data states, and is responsible for data interaction and storage.

  • Views include the UI presented to the user and all other interactions with the user;

  • The Controller includes the 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 means of communication between parts

v-c-m-c-v

Interactive mode

There are two ways that MVC can take instructions from a user. One is to take instructions from the View and pass them to the Controller.

The other is to receive instructions directly through the controller.

The instance

  1. The user can send commands (DOM events) to the View, and the View directly asks the Model to change its state.

  2. Users can also send instructions directly to the Controller (changing the URL to trigger a hashChange event), which the Controller sends to the View.

  3. The Controller is very thin and only serves as a route, while the View is very thick and the business logic is deployed in the View. Backbone simply cancels the Controller and keeps only one Router.

MVP (Model-View-Presenter)

  • Model: Responsible for network requests

  • Presenter: Handles data processing after a request to the network: loads successfully or fails to cancel loads

  • View: XML +Activity/Fragment displays the interface

The disadvantage of MVP: MVP adds a large number of interfaces and classes, which is inconvenient to manage, while the implementation code is concise

To solve this problem, we use a Contract (Contract; The contract; Protocol) manages constraints on Model, Presenter, and View to facilitate class search and maintenance in the later period.

The communication between the parts is two-way.

The View and the Model are not connected to each other, but are passed by a Presenter.

A View is very thin and doesn’t deploy any business logic. It’s called a Passive View, which means there’s no initiative. A Presenter is very thick and that’s where all the logic is deployed.