The default for Android is MVC:
- View: Corresponds to a layout file
- Model: Business logic and entity model
- Controllor: Corresponds to activity
But there are many problems:
1. This view corresponds to the layout file, actually can do very few things.
2. All event-handling code is in activity, causing the activity to be both view-like and controller-
So there's the MVP, this p, presenter, who sees actvity as the view layer, presenter is responsible for completing the interaction between the view layer and the model layer.
- View corresponds to activity, which is responsible for drawing the view and interacting with the user
- Model is still business logic and entity model
- Presenter is responsible for the interaction between view and model
This mode reduces the activity's responsibilities, simplifies the code in the activity, and extracts the complex logic code into the presenter for processing. The corresponding benefit is that the coupling degree is lower and more convenient to test.
The core idea of the MVP model:
The MVP abstracts the UI logic in the activity into the view interface, abstracting the business logic into the presenter interface, the model class or the original model.
This is the MVP model, and now, the activity is simple enough to respond to the life cycle, and the rest of the work is thrown into the presenter. As you can see, presenter is the bridge between the model and the view, in order to make the structure easier, the view does not directly manipulate the model, which is the biggest difference between MVP and MVC.
The above simple MVP Model UML diagram shows that using MVP, at a minimum, needs to go through the following steps:
-
Create the Ipresenter interface, place all of the business logic's interfaces here, and create its implementation Presentercompl (where you can easily see the business functions, because the interface can have many implementations so it is also convenient to write unit tests)
-
Create the iview interface, put all the view logic interfaces here, its implementation class is the current activity/fragment
-
As can be seen from the UML diagram, the activity contains a ipresenter, and Presentercompl contains a iview and relies on the model. The activity only retains calls to Ipresenter, and all other work is left to Presentercompl.
-
Model is not necessary, but there will be a view and presenter
As described above, MVP's main feature is to pull away many of the logic in the activity into the view and presenter interface, and by the specific implementation class to complete. This is a lot more iview and Ipresenter interfaces.
If you need to add new business logic, just add it to ipresenter, then implement it through its implementation class Presentercompl, and finally call Ipresenter abstract method in activity
If you need a new UI, you need to extract it into the iview, the activity to implement the abstract method, in the Presentercompl can call iview abstract method.
Loginpresentercompl retains the Iloginview reference, so the UI can be manipulated directly in Loginpresentercompl instead of being done in the activity. Instead of using activity directly, the Iloginview reference is used here, so if the same business logic needs to be used in other activities, You can directly reuse the Loginpresentercompl class (an activity can contain more than one presenter, in short, what business needs to be new what kind of presenter, which is the core idea of MVP)
by iview and Ipresenter, the activity is separated from the activity UI Logic Business Logic just does its basic job! as to model, or the original MVC model.
All logic is not implemented in the activity.
Ref: 1190000003927200
Android MVP Understanding