Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement MVP pattern in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to achieve the MVP model in Android. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

MVC

I believe you are all too familiar with the MVC model. If you are still unfamiliar with the MVC pattern, it is recommended that you understand MVC thoroughly before moving on. Because the MVP pattern can be said to have evolved from the MVC pattern in Android development.

The full name of MVC is Model-View-Controller, which is the abbreviation of Model-View-Controller. MVC begins to exist in desktop programs. M refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V in order to maintain and update the subsequent programs.

For native Android projects:

M (Model): do not simply understand Model as an entity class (Entity), Model should contain two parts of functions, one is to deal with business logic, such as some Helper classes, and the other is to provide data displayed by View, such as some Java Entity classes.

V (View): responsible for drawing UI elements and interacting with users. It generally refers to the xml layout file in the layout directory.

C (Controller): logical processing that handles interaction with the user. It means Activity or Fragment.

Why use MVP

We often perform control initialization and handle control click events in Activity or Fragment, that is, executing the findViewById () method and the setOnClickListener () method. This leads to the fact that Activity (Fragment) is not just Controller. With the increasing complexity of the interface and its logic, the responsibilities of Activity (Fragment) are increasing, which will make Activity (Fragment) very bloated. Come to think of it, if there are thousands of lines of Activity or Fragment code, not to mention adding new features, even maintenance is a little disgusting. In order to lighten the burden of Activity (Fragment), the MVP model was born.

MVP

MVC, whose full name is Model-View-Presenter, is an acronym for Model-View-Presenter. Presenter acts as a bridge between Model and View, and is responsible for combining the corresponding Model and View.

For native Android projects:

M (Model): the same function as in MVC mode

V (View): refers to Activity or Fragment, which is responsible for initializing the UI element. It is recommended that the UI element be associated with the Presenter.

P (Presenter): logical processing that handles interaction with the user. Presenter accepts data from the model layer, processes it, and returns it to the VIew layer. Presenter and View (Activity) interact through the interface (Interface).

Take antoniolg's androidmvp open source project as a chestnut explanation.

There are ten classes in the project directory, so I will use the login package as a breakthrough to explain the idea of MVP implementation.

There are two classes LoginActivity and LoginView in the View layer. LoginActivity is the login interface class; LoginView is an interface class that interacts with UI based on the results of data processing by the LoginPresenterImpl class.

1) LoginView mainly contains methods to update UI so that the LoginPresenterImpl class holds its reference. This allows the LoginPresenterImpl class to update the UI through LoginView. The specific code is as follows:

2) the main work of the LoginActivity class is to initialize UI and implement the method to update UI. The code is as follows:

There are two classes LoginPresenterImpl and LoginPresenter in the Presenter layer. LoginPresenterImpl is the implementation class of the LoginPresenter interface; LoginPresenter is also an interface class.

1) LoginPresenter contains methods to validate login information and destroy objects. The LoginActivity class can pass data to the LoginPresenterImpl class through it. The specific code is as follows:

2) the LoginPresenterImpl class implements methods to validate login information and destroy objects, but its internal authentication information is implemented by the LoginInteractorImpl class. The specific code is as follows:

The classes of the model layer are: LoginInteractor and LoginInteractorImpl. LoginInteractor is also an interface. The LoginInteractorImpl class implements the methods in LoginInteractor

1) the method in LoginInteractor is to actually process the data, that is, to verify that the information conforms to the standard. LoginInteractor returns the results of the data processed by LoginInteractorImpl to LoginPresenterImpl.

2) when LoginInteractorImpl verifies the login information, it performs null operation. If you need to request the server to determine whether the login was successful, then the network operation is also performed here.

If you can take down the above code, then you already understand the MVP pattern. You can get through the MVP by going through the rest of the code yourself and citing examples.

Advanced level

The responsive programming library RxJava has become very popular, and its advantage is to greatly reduce the amount of code. MVP and RxJava can be seamlessly docked, and the two are a perfect match.

The above is how to achieve the MVP pattern in the Android shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report