The implementation of Android MVP is analyzed with an example

Recently, I read the source code of the project and found that there are traces of MVP in the project, but I can't well understand the relevant code implementation logic. The main reason is that my understanding of MVP is too conceptual and has not been really operated. This paper intends to analyze a simple example of MVP to help me better understand the internal idea of MVP.

For what is MVP, the difference between MVP and MVC, and the advantages of MVP, you can refer to this article: a simple and understandable introduction to MVP mode. There is also a demo in the article, which can help you better understand.

Today's analysis is a demo written by others. In fact, the author also wrote an article to introduce (Android MVP with fragment and recyclerview). Why should I analyze it myself? In fact, I have applied the idea of MVP to a demo I wrote by imitating this demo. However, after a long time, I forgot, so I plan to sort it out. Of course, the article will not have to put forward its own ideas like the author's article. The demo is also transformed and optimized to make it more in line with the idea of MVP.

It is recommended to read the first two articles and then this article, so as to better understand the content of the article.

First, let's look at the structure diagram of the code. You can see that there are six folders, and the latter three folders are related to the MVP pattern. The data related classes are stored in the model. Picture is a data model, and several other classes are responsible for downloading picture to obtain data. The presenter will introduce references to model and view to control model and view. There is only one pictureview class in view, but strictly speaking, picturefragment and pictureadapter should also be placed in the view folder, but this is also possible.

What this project needs to do is very simple, that is, download pictures from the network, display them on the mobile phone, click the pictures, and a toast will pop up.

  MVP ? Here, M is not a picture, so there must be a picture entity class. The picture needs to be downloaded. Therefore, a class should be established to control it, but the final call to download is in P.

What about v? It's a fragment, which is composed of recyclerview and ProgressBar. Pictureview is an interface used to control the display of pictures. But it is also called in P.

Finally, P encapsulates the operations on M and V, that is, the class picturepresenterimpl.

Implementation of M (only paste important code):

Look at the above code. Only loadpictures comes from the interface. Why doesn't the createpictures method be written in the interface? The main reason is that the method of writing on the interface is invoked in P. If you don't need external calls, you don't need to be in the interface.

Implementation of V (only paste important code):

Although there is only one class pictureview in view, you can clearly know what the view needs to do from this class.

Implementation of P (only paste important code):

It can be seen that in P, the logic of M and V is encapsulated and controlled uniformly.

Finally, let's talk about fragment. P is introduced internally. Mainly because P cannot control the life cycle, it is necessary to use the life cycle of fragment to control the whole process.

After reading the above demo, you don't think that there are V and P in the fragment, which is actually not conducive to maintenance. Especially when there are more and more views in the later stage, you have to write the initialization of views in the fragment at that time? Therefore, the next step is to reduce the content of fragment. How to lose weight? See below for details.

In the modified structure, only a basepageview is created in the view to handle the initialization and control logic of the view.

Its code is as follows:

In this way, when you need to change the view, you only need to change this class instead of running to the fragment.

At the same time, fragment also succeeded in slimming down:

After the transformation, is it better understood. When we need to modify a part, we can easily locate the place to be modified.

Well, after the transformation, I believe everyone's understanding of MVP will be more profound.

I hope this article is helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>