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

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.

主站蜘蛛池模板: 新建县| 普安县| 定南县| 徐汇区| 益阳市| 灯塔市| 东城区| 鲜城| 咸宁市| 民丰县| 昂仁县| 上林县| 平塘县| 马边| 汉中市| 曲水县| 茌平县| 敖汉旗| 莱西市| 双柏县| 介休市| 甘肃省| 安泽县| 六安市| 闸北区| 青浦区| 广南县| 仪陇县| 玛沁县| 绿春县| 遵义市| 尉犁县| 宿松县| 石河子市| 阜南县| 乌鲁木齐县| 沧源| 登封市| 塘沽区| 恩平市| 万山特区|