Android: MVP. Right way to communicaticate between components

958 views Asked by At

I have implemented MVP pattern in my application. I have an additional layer which I call Repository, which is responsible for running HTTP asynchronous requests. I have created OnTaskCompleteListener interface for communication between Repository and Model (Model implements OnTaskCompleteListener). So when Repository finishes the request, it calls a method in Model and updates data. Then, Model calls a method in Presenter with the same mechanism, to let it know that Model has been updated. What I am worrying, is the chain of callbacks that comes from Repository up to Presenter. Is there a better way to communicate between components of MVP or is it the way to go? I did try "EventBus", but a large number of needed interfaces scared me off. Observer did not seem fit as there is only one listener for each component (Model listens to Repository, Presenter listens to Model). Thank you.

1

There are 1 answers

0
Sermilion On BEST ANSWER

After extensive reading, I came to a conclusion, that I do not need another class 'Repository' to delegate loading of data. The model actually should be responsible for loading and holding data. Using a callback method parameter for the method of the presenter, that calls a method that loads data in the model is a great way to communicate between model and presenter.

Presenter.java

@Override
public void loadData(){
    mModel.loadData(new Callback{
        void onSuccess(){
            getView().notifyDataLoaded();
        }

        void onError(){
            getView().notifyErrorOccured();
        }
});

This way model, presenter and view are linked in this single method in very clear and intuitive manner. Hope this will help someone.