Summarize the android MVP and MVC design patterns and interface-oriented programming

I. Overview

The predecessor of the MVP design pattern is MVC, this need not be discussed

The corresponding relationship of MVC in the Android project is as follows:

Layout->View: Corresponding to the layout file
Activity->Controller, View (where activity is not particularly clear)
various business logic entity classes->Model 

The basic concept of MVP is:

MVP refers to Model, View, Presenter (Interactor/Presenter), which evolved from the classic pattern MVC. Their basic ideas are similar: Controller/Presenter is responsible for logical processing, Model provides data, and View is responsible display.

Second, the difference between MVP and MVC

There is a major difference between MVP and MVC: View does not directly use Model in MVP. The communication between them is carried out through Presenter (Controller in MVC). All interactions occur inside Presenter, while in MVC View will read data directly from the Model instead of through the Controller.

The difference between giving a picture the most cash out:

The upper part of the figure is the MVP mode-it can be seen that View and Model do not directly interact, but use the presenter as an intermediary to coordinate work. The advantage of this is to make the code clearer and clearer.

The lower part of the figure is the MVC pattern-the controller (Activity) directly manipulates the view and the Model, which means that the running View and the Model communicate directly.

The advantages and disadvantages of MVP and MVC are summarized as follows:

1: MVC allows View and Model to communicate directly.
2: In MVP, the interaction between View and Model is done inside Presenter
3: In MVP, View usually abstracts a series of interfaces. (Interface-oriented programming)
4: Compared with MVC, MVP greatly reduces the degree of coupling (Activity no longer performs complex operations), the level is more obvious, and is more conducive to unit testing.
5: Disadvantages of MVP: Class files will explode (extremely increase), and there is a certain learning cost.


3. MVP design principles - how to realize an MVP design pattern?

1: An Activity corresponds to a View (use the activity as an MPV view for operation)
2: Normally, a View corresponds to a Presenter. When the business is complex, a View can correspond to multiple Presenters.
3. Normally, the View and Presenter is abstracted into an interface.

Mvp protocol-oriented programming framework application

mvp is an architectural model. We can usually separate the view and data in Android and use the presenter to manage it. The data and model cannot communicate directly. The fundamental difference with mvc is that there is no intersection between the view and the model.

However, in the huge Android source code, it may not only be the separation of view and data model, but also the separation of many presenters. This will produce an mvp architecture pattern between functions. The rootPresener manages many childPresenters. Programming like protocol is here. We can take a look at the source code of some part of Android (ViewManger)

Write picture description here

Benefits of protocol-oriented programming

  1. Adding extended functions makes it easier to ensure the safety of specific implementation code
  2. Add a new way of passing parameters (code block)
  3. Easier to realize the idea of ​​low coupling
  4. Separate the specific implementer from the implementer

Guess you like

Origin blog.csdn.net/shu_quan/article/details/74561205