- Expert Android Programming
- Prajyot Mainkar
- 876字
- 2021-07-08 10:29:15
MVP (Model View Presenter)
Model View Presenter (MVP) is derived from the MVC pattern. MVP is used to minimize the high dependency on the view, which is the case in the MVC. It separates the view and model by using the presenter. The presenter decides what should be displayed on the view.
- Model: The model represents the objects in the application. This has the logic of where the data is to be fetched from.
- View: The view renders information to users and contains a UI Component .xml file, activity, fragments, and dialog under the View Layer. It does not have any other logic implemented.
- Presenter: The presenter layer performs the task of the controller and acts as the mediator between the view and model. But unlike the controller, it is not dependent on the view. The view interacts with the presenter for the data to be displayed, and the presenter then takes the data from the model and returns it to the view in a presentable format. The presenter does not contain any UI components; it just manipulates data from the model and displays it on the view.
The interaction between the various components of the MVP are shown in the following figure:

In the MVP design, the presenter communicates with the view through interfaces. The interfaces are defined in the presenter class, to which it passes the required data. The activity/fragment or any other view component implements the interfaces and renders the data in a way they want. The connection between the presenter and the view is one to one.
The following provides an example to enable a better understanding of the MVP design:
In this example, we will show how the MVP design can be used to display a list into a recyclerview:
public interface LocationInteractor { interface OnLoadFinishedListener { void onSuccess(List<LocationItem> items); void onFailed(); } void loadLocations(OnLoadFinishedListener listener); }
Here we have a LocationInteractor class that is used by the presenter to communicate with the model:
public class LocationInteractorImpl implements LocationInteractor { @Override public void loadLocations(final OnLoadFinishedListener listener) { RetroInterface.getRssFeedApi().getFeed("", new Callback<LocationResponse>() { @Override public void success(LocationResponse locationResponse, Response response) { if (listener != null) { if (locationResponse != null) { listener.onSuccess(locationResponse.getDetails()); } else { listener.onFailed(); } } } @Override public void failure(RetrofitError error) { if (listener != null) { listener.onFailed(); } } }); } }
This is the LocationInteractorImpl class which is the model. This class interacts with the presenter to provide the list of locations.
The loadLocations function is used by the presenter to interact with the model and fetch the list of locations.
The model (LocationInteractorImpl) then uses the listener.onSuccess and listener.onFailed methods to send results back to the presenter:
public interface LocationInterface { void showProgress(); void hideProgress(); void locationLoaded(List<LocationItem> items); }
The LocationInterface class is used by the presenter to communicate with the view:
public interface LocationPresenter { void loadLocations(); void onDestroy(); }
The LocationPresenter class is used by the view to communicate with the presenter. Depending on the functions called by the view, the presenter will communicate with the model and get the responses:
public class LocationPresenterImpl implements LocationPresenter, LocationInteractor.OnLoadFinishedListener { private LocationInterface locationInterface; private LocationInteractor locationInteractor; public LocationPresenterImpl(LocationInterface locationInterface) { this.locationInterface = locationInterface; this.locationInteractor = new LocationInteractorImpl(); } @Override public void loadLocations() { if (locationInterface != null) { locationInterface.showProgress(); } locationInteractor.loadLocations(this); } @Override public void onDestroy() { locationInterface = null; } @Override public void onSuccess(List<LocationItem> items) { if (locationInterface != null) { locationInterface.locationLoaded(items); locationInterface.hideProgress(); } } @Override public void onFailed() { if (locationInterface != null) { locationInterface.locationLoaded(null); locationInterface.hideProgress(); } } }
The LocationPresenterImpl class is the presenter class which communicates between the view and the model. This class implements LocationPresenter with which the view communicates with the presenter and the LocationInteractor.OnLoadFinishedListener from which the model communicates with the presenter.
When the view calls the loadLocations function of the presenter, the presenter interacts with the model and calls the loadLocations method of the model, indicating that the model is to return the list of locations to be displayed.
The onSuccess and onFailed functions are then called by the model after the list has been fetched successfully or has failed. Then the presenter communicates with the view through the locationLoaded function. Here, it passes the list that has been fetched by the model:
public class LocationListActivity extends Activity implements LocationInterface{ private ProgressBar progressBar; RecyclerView recyclerView; List<LocationItem> items; AddPlacesAdapter adapter; private LocationPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); progressBar = (ProgressBar) findViewById(R.id.progress); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); items = new ArrayList<>(); adapter = new AddPlacesAdapter(this, items); adapter.setClickListener(new AddPlacesAdapter.ClickListener() { @Override public void onItemClickListener(View v, int pos) { //Handle Click events } }); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); presenter = new LocationPresenterImpl(this); presenter.loadLocations(); } @Override protected void onDestroy() { presenter.onDestroy(); super.onDestroy(); } @Override public void showProgress() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideProgress() { progressBar.setVisibility(View.GONE); } @Override public void locationLoaded(List<LocationItem> items){ if(items!=null) { this.items = items; adapter.refresh(items); } } }
The LocationListActivity is the view where all the interaction with the user will happen. The view implements the LocationInterface, by which it gets the responses from the presenter. The view uses an instance of the presenter:
presenter = new LocationPresenterImpl(this);
It then calls the presenter.loadLocations(); to tell the presenter that it wants the list of locations to be displayed.
- DB2 V9權威指南
- 自然語言處理實戰:預訓練模型應用及其產品化
- Neo4j Essentials
- Building Mapping Applications with QGIS
- Hands-On Enterprise Automation with Python.
- 深入淺出Serverless:技術原理與應用實踐
- Node.js Design Patterns
- Python數據結構與算法(視頻教學版)
- Java Web開發就該這樣學
- 一本書講透Java線程:原理與實踐
- Unity 2018 Augmented Reality Projects
- uni-app跨平臺開發與應用從入門到實踐
- C Primer Plus(第6版)中文版【最新修訂版】
- C語言編程魔法書:基于C11標準
- 大話程序員:從入門到優秀全攻略