GENERIC-9: Use exclusively interfaces to let app modules communicate. This protects the architectural structure and helps defining a clear responsibility of modules.


Description

Using interfaces for linking modules, is a great way for module communication. This way modules can’t access methods that are not declared in the interface. And helps define a clear overview of what each module does. Using interfaces for linking modules, is a great way for module communication. This way modules can’t access methods that are not declared in the interface. And helps define a clear overview of what each module does.

Example

We created a simple application which retrieves employees from this url: http://dummy.restapiexample.com/api/v1/

If we take a look at the Presenter and Repository modules, we see they communicate exclusively via interfaces. The MainPresenter implements the OnViewEmployeesFinishedListener . Which contains the methods:

  • onError Which will be called when an error occurs in the Repository
  • onSuccess Which will be called when the Repository successfully retrieves the employees

The MainPresenter calls the employeeRepository.getList(this) giving itself as parameter. The Repository communicates back by calling the methods of the OnViewEmployeesFinishedListener interface. OnError() is called if there does not exists a cache and the service does not work. OnSuccess() is called if the data is successfully retrieved from cache, or is newly retrieved from the network.

Check out the Github page to view the complete repository.