As an evolutionary version of MVC, MVP has attracted more and more attention in Android development. However, it needs to be cautious to choose such a software design mode in project development. Once you decide to use MVP as your App development model, you'd better stick to it. If problems are found in the development process of using MVP mode and the pit is getting bigger and bigger, then you want to redesign with MVC, which is basically equivalent to reinventing. You should know that there is no unified standard or framework for MVP on Android, unlike SSH, which is a mature, stable and powerful Three Musketeers supporting Java EE development. Therefore, when using MVP, you must do your own understanding and try to predict the requirements of each module of your App (the customer said that we would change:-() in order to do the design work well in advance. Of course, since MVP can appear, it must have its advantages. Who else will pay attention to this pop-up thing? Here are some explanations about MVP in Android.
Introduction to MVP
I believe everyone is familiar with MVC: M-Model- model, V-View- view, C-Controller- controller. As an evolutionary version of MVC, MVP is also a way to realize the user interface (user layer), so the corresponding meanings of similar MVP are: M-Model- model, V-View- view, P-Presenter- presenter. From the combination of MVC and MVP, controller/Presenter plays the role of logical control processing in MVC/MVP, and controls all business processes. The biggest difference between MVP and MVC is that M and V are not directly related, and model and view are not directly related. There is a renderer layer between them, which is responsible for regulating the indirect interaction between views and models. The structure diagram of MVP is shown below, so it is not necessary to limit the understanding of this diagram to rules. After all, there will be some differences between different scenes. An important point in Android is that UI operation basically needs to be done asynchronously, that is, UI can be operated in MainThread, so it is reasonable to cut and separate the view from the model. In addition, by using the interface to define interactive operations, the interaction between the presenter, the view and the model can be further loosely coupled, and unit testing can be carried out more conveniently through the interface.
? MVP structure diagram
MVP model
A model is an abstraction of the data that the user interface needs to display, and it can also be understood as an abstraction from business data (results) to the user interface (business rules, data access, model classes). In this paper, Demo directly puts the business into the corresponding model for simple processing.
MVP view
This layer of view is very thin, only responsible for displaying data and providing a friendly interface to interact with users. The activities and clips under MVP are reflected in this layer. Activity generally does some work such as loading UI view, setting monitoring, and handing it over to Presenter, so it needs to hold a reference to the corresponding Presenter. For example, the Acionbar should be hidden or displayed when scrolling the activity list. In addition, when judging the data input in View, for example, if the input data of EditText is a simple non-empty judgment, it can be used as the logic of View layer, while when it is necessary to judge the data of EditText in a more complicated way, such as obtaining local data from the database, it obviously needs to be returned through Model layer, so these details need to be weighed by themselves.
The host of MVP
Represents the distribution of various logics of layer handlers. After receiving the feedback command, timing command, system command and other instructions, the UI of the view layer hands over the distribution processing logic to the business layer for specific business operations, and then displays the obtained model to the view.
demonstrate
Let's start writing code. Demo is very simple, but the picture above is more intuitive. Enter the city code, click the button to get the weather information of the city, and then display it. The network operation uses the volley frame, the analysis uses Gson, and the rest is handwritten. The packaging design of the whole project is as follows:
? Packet structure
Preview of project effect
There are three obvious layers in the package diagram: model package, Presenter package and UI package, all of which have realized their own structures. The model is the WeatherModel, the Presenter is the WeatherPresenter, and the view is the Weather, so the specific implementation class is in the impl package, and the view layer is the Activity. In addition, app has nothing to do with util package and can be ignored. It can be seen that there are obviously many things in the project after adopting MVP design, which is inevitable. The original method can make the project easier to open, but there will be maintenance, testing and adding functions in the future. . .
The entity attributes in Entity correspond to those in json, so the code will not be posted.
Interface in view:
Common interface weather view {
void show loading();
void hide loading();
void show error();
Void setWeatherInfo;
}
Interface of WeatherPresenter:
Common interface WeatherPresenter {
/**
* Understand the logic of weather.
*/
Voidgeweather (string cityno);
}
WeatherModel interface:
Common interface WeatherModel {
Void loadWeather (string cityNO, onWeatherListener);
}
There is also an OnWeatherListener in prestener, which is implemented in Presenter layer, and calls back the model layer to change the state of the view layer to ensure that the model layer does not directly operate the view layer. If this interface is not implemented in weatherpresenttempl, and weatherpresenttempl only has references to View and Model, how can Model tell the result to View? Of course, this is just a solution. In practical projects, third-party frameworks such as Dagger, EventBus and Otto can be combined to realize more loosely coupled design.
WeatherListener {
/**
* callback on success
*
* @param weather
*/
Void onSuccess;
/**
* Call back when it fails, handle it simply, and do nothing.
*/
void on error();
}
Therefore, the code flow of demo: Activity has done some UI initialization, and it is necessary to instantiate the reference corresponding to WeatherPresenter and the interface that implements WeatherView, and monitor the interface actions. After the Go button is pressed, the weather query event will be received, and when it is received in onClick, it will be handed over to WeatherPresenter for processing through the reference of WeatherPresenter. When WeatherPresenter receives the logic of weather query, it knows that it needs to query the weather, and then hands over the specific business implementation of weather query to WeatherModel for implementation, and at the same time passes the WeatherListener itself to WeatherModel. After the WeatherModel queries the weather service, it informs the WeatherPresenter of the result through the WeatherListener callback, and the WeatherPresenter returns the result to the activity in the view layer, and finally the activity displays the result. That's it. Please tap the brick.
end
Which software design mode should be adopted to achieve the following goals, and it is best to find the right one to use:
Easy to maintain
Easy to test
Loose coupling degree
High reusability
Strong and stable
-
The above contents refer to the Internet, and I hope the above knowledge will help you.