Android MVP framework learning and practice

Android MVP framework learning and practice

Preface

I thought that Internet companies basically have ready-made projects, and the most I could do as an intern was to modify the existing code and add functions. So I joined a medium-sized outsourcing company, hoping to experience the process of a commercial project from landing to completion. After joining the company, I was assigned to a project for a foreign contractor (I'm just an intern, and my first project is for a foreigner, is that okay?).

As a rookie with experience in Android development, and who has won some awards in various service outsourcing competitions (although such competitions in college are not very easy, there is still some development work, haha), I have some understanding of the composition of a project, how to develop, etc. Due to my previous experience in competition projects, I developed the Android side independently, so I did whatever was convenient for the project architecture and framework, and did not consider the readability and reusability of the code (I only found out after entering the company for internship that these things are very important). For convenience, I put the business logic and view binding into the activity class, which made the activity class too bloated, which brought great inconvenience to the reading and modification of the code. However, due to my lack of project experience, I dug a hole for myself from the beginning, which was not only difficult to fill later, but also got deeper and deeper (covering my eyes with one hand and crying with laughter.jpg). Fortunately, I was the only one developing it, otherwise, if it was collaborative development, I would probably be hammered by my friends.

After joining the company, I got the basic development framework of the project from the Android team leader.

After a little thought, I think that this presenter should be the P in MVP that I have seen in books before, considering my level of passing the Level 6 exam without preparation. I can find some information online to learn from it.

Let’s get to the point.

MVP is the abbreviation of Model, View, Presenter. Model stands for model, which is mainly responsible for data loading; VIew stands for view, which is mainly responsible for view binding, interface display, interface logic jump, etc.; Presenter is the presenter, which mainly acts as the middleman between View and Model to complete the interaction between them, thus realizing the idea of ​​high cohesion and low coupling. Draw a relationship diagram between them.

Maybe this is a bit abstract, talk is cheap, show you the code.

Let's first take a look at the MVP framework implementation of our project team's Android boss.

MainActivity.java:

MainContract.java:

MainPresenter.java:

The boss is really a boss. He has encapsulated various base classes to facilitate the development of us newbies. (The above code also uses RxJava. Emmmm, in fact, this is a technology that I have not come into contact with as a newbie. I will also update the RxJava-related learning blog later. I must work hard to become stronger~).

We will find that the above three .java files can actually implement the complete logic of an interface, but we have View and Presenter, but where is Model? And what is Contract? Is this still the MVP design pattern?

At first, I was confused, so I went to learn from the experts. I learned that there is actually no unified standard for the MVP design pattern of Android. There are many ways to implement the MVP concept. The method shown above is an MVP implementation method officially proposed by Google.

In the official Google MVP implementation, the role of Model is weakened and Model is integrated into Presenter. We can see that the getBook method in MainPresenter is actually the implementation of Model. The Contract class is translated into Chinese as the contract class. The word contract is very vivid. As the interface between view and presenter, it defines the methods to be implemented on view and presenter. Then view and presenter implement their methods respectively, making the logic of the entire interface quite clear.

Since the code of the big brother has been encapsulated, some details have been hidden, so let's make a small demo and use Google's official MVP implementation method to implement a program that simulates pulling information from the server and displaying it on the interface, so as to understand MVP more intuitively.

My performance

Let’s take a look at the interface presented by *** first.

Interface display

The interface layout is very simple, with just a TextView and a Button. I won't post the layout file xml. The main function is to click the button to get data, simulate getting data from the server, and then display it on the textview.

The following code structure consists of Activity (that is, View), Presenter, and Contract:

Code structure

For the sake of convenience, let's first take a look at the contents of MainContract:

MainContract Code

It is quite simple, because Contract is a contract class, which does not implement any functions. It only states the interfaces that Presenter and View need to implement respectively. I defined the LoadData method in the Presenter interface. MainPresenter needs to implement the LoadData method in the Presenter interface to complete the data pulling from the server; the onDataLoaded method is defined in the View interface. Similarly, because MainActivity applies the View interface, the onDataLoaded method needs to be implemented.

MainActivity:

MainActivity Code

MainActivity is mainly for binding interface components and initializing some member variables. You can see that I have initialized textview, button and a presenter here, and then the data operations on the interface are implemented through the presenter.

We can see that the onDataLoaded method is overridden here. Because MainActivity implements the View interface in the contract class, the onDataLoaded method in the interface needs to be implemented. The main function of this method is to present the data obtained in the presenter on the interface. In our case, the data is displayed on the textview of the interface.

MainPresenter:

MainPresenter Code

MainPresenter is where the interface data operations are implemented. It implements the Presenter interface of the contract class, so it is necessary to implement the LoadData method defined in the Presenter interface. The function of this method is to obtain data from the server and then call back to the Activity (that is, View). I was lazy here and did not obtain data from the server. I hard-coded the data directly (hehe). Well, I pretended to obtain the data from the server, and then called the onDataLoaded method of the View to pass the obtained data to the activity. In this way, the data can be updated in the activity.

Oh, yes, there must be a member variable of View in Presenter, MainContract.View mView; otherwise, you cannot call the methods on view. The initialization of mView is copied when the Presenter variable is created, which can be seen in MainActivity

Take a look at the results of the operation

Result after clicking the button

At this point, we have implemented a very simple app based on the MVP framework.

If you need to add functions later, just add the interface of the required functions to the Contract, and then implement them in the Presenter and Activity respectively.

For example, if I want to add a new function A, I need to make corresponding interface changes on the View after execution. We only need to do this:

1. Define the functionA method in Presenter and the onFunctionAFinished method in View in Contract

2. Implement the functionA specific method in the Presenter implementation class and call onFunctionAFinished to update the interface

3. Implement the onFunctionAFinished method in Activity

In this way, another function has been added. Isn't it very convenient and the logic is also clear? Activity is only responsible for the interface update part, and the acquisition of data is all implemented by Presenter. High cohesion and low coupling are very helpful for interface maintenance and development.

Of course, the above is just the simplest implementation of MVP. For ease of understanding, I have removed the definition and encapsulation of many base classes. It is impossible to be that simple in actual development. However, if you understand the idea of ​​MVP, the implementation is still very clear.

Here is the github address of the code above. Although there are only a few lines of code, it would seem unprofessional if I don't post it. I will not post the company's development framework code. I will only post my simple code.

https://github.com/reggie1996/mvp_demo

<<:  The prices of the first batch of 5G mobile phones were exposed, and netizens said: I can’t afford it!

>>:  Baidu Smart Mini Program and Evangelist Program "Strongly Support" Developers

Recommend

High scores can make money? Why do mobile phone manufacturers cheat on scores?

Many mobile phone users often run benchmarks when...

@All freshmen, how is your sun protection?

Military training is the beginning of the ivory t...

Foreign media criticizes iPhone X: no advantage over Android

iPhone X may be the most upgraded Apple phone in ...

Facing a sales ban in China: Is Apple on its way to the end of the patent war?

On the evening of December 10th, Beijing time, Qu...

New Media Operations: How to create a popular article opening?

In this era of impetuous information explosion, t...

5 major trends in brand marketing in 2021

Among the top ten popular slogans, Yuanfudao (A t...

Prozac is still unsolved: Where is the future of new antidepressant drugs?

In 1954, the first compound with antidepressant e...