Detailed explanation of how Android implements and encapsulates an MVP
preface
MVP evolved from the classic mode MVC, and their basic ideas have similarities: Controller / presenter is responsible for logic processing, model provides data, and view is responsible for display. The following article mainly introduces the relevant contents of Android from implementation to encapsulation of MVP, which can be shared for your reference and learning. Let's take a look at the detailed introduction.
Links between MVPs
The simple explanation is that M - > module processes data, V - > act displays the interface, and P - > the communication channel between M and V, that is, P is used to connect the data and the interface, so that the sub interface and data can be completely independent. Act only does things related to the interface, module only processes data, and P is only responsible for the communication between the two, so as to realize decoupling.
Simply implement an MVP without any encapsulation
Take the login interface as an example. The required files are as follows
Iloginview loginpresenter needs to interact with loginact. For example, the presenter needs to get the name through the act login box, and act needs the return value code after the presenter processes the login data
Irequestloginloginpresenter needs to interact with the loginmodule. For example, the presenter needs to get the login result through the module, and the module needs the presenter to pass his username and PWD, and pass an instantiated interface used to callback the return value
Irequestresultloginpresenter needs to get the return result of loginmodule in real time
After the three pipelines are defined, you can implement the theme modules, views and presenters of the three MVPs
Loginact implements the iloginview interface and holds the present object, so as to realize the interaction between loginpresenter and it. It can be seen that act has no place to process data. All it has to do is provide the name and password to the presenter, log in through the presence, and receive the return value code processed by the presenter.
Loginmodule is only used for data processing. For example, request the server through the name and PWD passed by the presenter to obtain the code and return it to the presenter through the interface
Loginpresenter is used as a bridge between the two. It holds two objects, one module and one view. It can be seen that it is a middleware to operate the module and view so that they can be connected. When act initiates the login operation, it obtains the name and PWD through the view, requests the server to obtain the return value through the module, and then passes it to act.
After the simple implementation, it is encapsulated
See that three interfaces are missing.. Because they are placed in the basecontract file, it is easier... Just think of a contract name.. Don't be so serious..
Although basecontract has no common factors, it still keeps a base in case of future demand.. Ibasemodule is used to realize the common points in all processed data. For example, all modules must request data to return a string
Baseact is used to encapsulate the common attributes of acts. For example, all acts implement an ibaseview interface and hold a presenter object. The presenter is instantiated in oncreate, and the module and view in the presenter are instantiated
Basemodule does not do any processing
Basepresenter holds both module and view, which are released during the actdestory
Act after base
As before, logincontract defines the data to interact with
Loginact eliminates the need to initialize the presenter
Loginmodule requests data and returns results with little difference
Loginpresenter eliminates the steps of instantiating module and view
To sum up, it is a simple process from implementation to encapsulation of an MVP... But the road to optimization is still long..
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.