preface

For more on the technology, see “I’m Hooked on Android, What’s wrong?” Let’s learn the basics of MVP.

You’re all familiar with the MVC architectural pattern. Today we are going to look at the MVP architecture pattern.

MVC vs. MVP

What is the Model View Presenter mode?

  • 1. In order for the view interface to interact with the model and controller, the controller performs some initialization events
  • 2. The user performs some operations through the view (user interface)
  • 3. The controller handles user behavior (which can be implemented using observed patterns) and notifies the model to update
  • 4. The model raises events to inform the view of changes
  • The view processes the event of a model change and then displays the new model data
  • 6. The user interface waits for further operations from the user

The advantage of the MVP

  • 1. The model is completely separate from the view, and we can modify the view without affecting the model
  • Models can be used more efficiently because all interactions take place in one place — inside the Presenter
  • 3. We can use a Presener for multiple views without changing the Presenter’s logic. This feature is useful because the view changes more frequently than the model.
  • 4. If we put logic in Presenter, we can test it out of the user interface (unit tests).

The problem of the MVP

Because rendering of a view is in a Presenter, the view and Persenter interact too often.

Another thing you need to understand is that if presenters render too much of a view, they tend to tie it too closely to that particular view. If the view needs to change, the Presenter needs to change too. For example, if a Presenter that was originally used to render Html now needs to be used to render Pdf, the view will probably need to change as well.

A simple login example

Effect:

The directory structure

MPV node structure

The server opposite shore use case tests LoginService

P

V layer

M layer

It looks a lot more complicated. The amount of code is relatively large. But if we use it in large projects we can show an advantage. Next comes the MVP encapsulation.

Over time, we can see that MVP can bring about tremendous things: In MVP, since the business logic is in PresenterTest, we can write a PresenterTest implementation class that inherits the Presenter interface. Now we can use PresenterTest as a unit test in our Activity. Just switch back after the test. If you find that you still need to test, switch to PresenterTest.

conclusion

What are the characteristics of M, V and P layers?

Model layer:

This layer is responsible for making requests to data sources (typically servers/databases, similarly below) and calling back data or error messages to the host. In addition to making a request, we generally need a way to cancel the request.

So the main functions of the Model layer are:

  1. Make a request to the data source;
  2. Cancel the request;
  3. Notify Presenter of processing results.

Presenter:

This layer is responsible for notifying the Model layer to make requests to the server and receive the data or error messages returned by the Model layer, and this layer is responsible for processing the data or error messages back to the View layer, which is responsible for display. Generally, network request error information is divided into two types. One is that the network status of the network device is incorrect and the request cannot be sent. Alternatively, the server rejects the request. So the main functions of presenters are:

  1. Notifies the Model layer to initiate a request to the server;
  2. Receive data returned by the Model layer (the server may return data or denial of service messages);
  3. Receive network error messages from the Model layer;
  4. Notify the Model layer to cancel the request;
  5. Notifies the View to receive processed data.

The View layer:

In MVP mode, the View layer is an interface. Its primary task is to upload the Presenter’s processed data to a specific native control for display and to control whether the loading progress bar is displayed. So the main functions of the View layer are:

  1. The progress bar is displayed or hidden.
  2. Receive correct data processed by Presenter.
  3. Receive network error messages returned by Presenter.
  4. Receives a server denial of service message returned by Presenter.

The core idea of the MVP model

The MVP abstracts the UI logic of the Activity into a View interface, abstracts the business logic into a Presenter interface, and the Model class remains the same.

In MVP mode, the Activity responds to the lifecycle and displays, and the Presenter layer does all the other work. The Presenter layer is a bridge between the Model layer and the View layer.

Project Address:

https://github.com/androidstarjack/MvpSimpleStudy

Server test project address:

http://download.csdn.net/download/androidstarjack/9966557

Reference links:

http://www.360doc.com/content/13/0808/13/7427585_305589280.shtml

Remaining issues:

  • If there is a lot of code, do you consider how the MVP is reused, and if it is reused, will it increase coupling?

Conclusion: Too much pursuit of patterns can sometimes backfire, and MVC applications sometimes have too much generality.

(Welcome to study and exchange)

MVP reference

MVP+Dagger2+Retrofit2.0+Rxjava look at this one example enough

MVP + + Rxjava Retrofit of actual combat

Believe in yourself, there is nothing impossible, only unexpected

Wechat official account: Terminal R&D Department

If you find it helpful, feel free to hit on me.