MVP-4: Clearly define contracts between Views and Presenters.


Description

Before starting to develop a new app feature, a good architectural practice consists in writing a contract documenting the communication between the View and the Presenter. The contract should document for each event in the View which is the corresponding action in the Presenter. By implementing contract interface classes, the source code of apps become more understandable, as the relation between the View and the Presenter is explicitly documented.

Example

For this example we will look at the Book example.

Our BookListContract:

Our BookListPresenter implements the Presenter interface and our BookListActivity implements the View interface. This contract helps to design how the communication between our BookListActivity and our BookListPresenter will be.

The Presenter will look the books, and the View will present the books.

At a certain moment our BookListActivity will request the Presenter to loadBookList();

This Presenter will then ask the client by calling the getBookList with its callback object as parameter.

If the request is succesfull, the callback will call the showBookList() with the List as its parameter. If not, it will show the error message.

Login Injection

The Contract describes the interaction between the view and the presenter. The Presenter has to perform two methods:

  • loadBookList to get the books for the view
  • dropView to release its connection with the view

The View has to perform four methods:

  • showProgress to add progressbar, to show that the books are being retrieved.
  • hideProgress to remove the progressbar, if successful or unsuccesful.
  • showBookList to show the retrieved books
  • showLoadingError to show if the retrieval was unsuccesful.

I hope that creating this contract makes the interaction more understandable.

Check out the Github page to view the complete repository.