j2emanue
j2emanue

Reputation: 62519

android MVP - Presenter with multiple model's

Planning on implementing MVP architecture for a MVC type android app. I have a concern on how I can make a presenter that will have multiple models.

Typically a presenter's constructor will look like this:

MyPresenter(IView view, IInteractor model);

This way I can swap out dependencies when I'm testing and mock out view and model easily. But imagine my presenter is tied to an activity that must be multiple network calls. So for example I have one activity that does an API call for login and then another one for security questions, and then a third one for GetFriendsList. All those calls are in the same activity theme. How to do this with the constructor I showed above? or what is the best way to do this kind of thing? Or am I limited to having just one model and calling the services within that one model?

Upvotes: 3

Views: 1672

Answers (1)

Nitin Jain
Nitin Jain

Reputation: 1344

Presenter constructor need only the view.You don't need to dependent on the model. define your presenter and a view like that.

 public interface Presenter{
  void getFriendList(Model1 model);
  void getFeature(Model2 model2);

    public interface View{
      void showFriendList(Model1 model);
      void showFeature(Model2 model2)
    }
  }

now your implementation class has dependent on view part only.

rest your method will handle your model

class PresenterImpl implements Presenter{
    View view;  
    PresenterImpl(View view){
     this.view = view;
    }
  void getFriendList(Model1 model){
   //Do your model work here
   //update View
   view.showFriendList(model);
  }
  void getFeature(Model2 model2) {
   //Do your model work here
   //updateView
   view.showFeature(model2)

  } 
}

Upvotes: 3

Related Questions