官术网_书友最值得收藏!

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:

Figure 2.2.2

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.

主站蜘蛛池模板: 昂仁县| 峡江县| 临漳县| 山阳县| 凌海市| 塔城市| 龙井市| 湖南省| 伊春市| 青田县| 湖州市| 鸡西市| 常熟市| 白城市| 白城市| 闵行区| 日土县| 肥城市| 固阳县| 连云港市| 吴江市| 突泉县| 东台市| 望城县| 尉犁县| 资兴市| 久治县| 竹溪县| 玉田县| 增城市| 清河县| 马尔康县| 全南县| 香河县| 赞皇县| 抚松县| 伊川县| 云南省| 沿河| 邢台市| 吕梁市|