Building Prowl using the MVP pattern

Refactoring our app to use MVP and how it benefited us

The only way to make the deadline — the only way to go fast — is to keep the code as clean as possible at all times.
- Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

At Treebo, we are trying to leverage technology to give every guest an experience of the highest quality during their time with us. Given this mandate, we have built Prowl — The Guest Experience Platform @ Treebo, a complex system that allows us to track quality at various properties in real time (We have detailed it at a granular level here). An integral part of ensuring that every Treebo property meets our quality standards, is the Prowl android app.

Ever since the first version of Android was launched in 2008, the age old question plaguing developers was, ‘How do we architect the app such that adding new features and maintaining the existing code would be much easier?’ An Activity or a Fragment is the base component for any android application. However, when only dealing with activities, it often becomes a very complicated task to separate out the UI logic and the business logic, which violates the Single Responsibility principle, the first SOLID design principle. We thus need an architectural pattern where the UI (View) and data (Model) are independent of each other and are connected via a common interactor (Presenter).

What is MVP?

  • View is a layer that displays data and reacts to user actions. On Android, this could be an Activity, a Fragment, an android.view.View or a Dialog.
  • Model is a data access layer such as database API or remote server API.
  • Presenter is a layer that provides View with data from Model. Presenter also handles background tasks.

With the help of this pattern, it becomes easier to segregate the View logic from the Business logic. This separation of concerns helps write scalable applications, makes testing much easier, makes code maintenance much simpler and is huge blessing for developers.

Why use MVP?

Most of the modern Android applications just use View-Model architecture. In a typical Model-View design pattern, the views and data layers of the application are tightly coupled and hence there is no layer of abstraction between the two layers. Therefore, for all practical purposes, there is only one layer handling both the views and their interaction with different data sources. This leads to redundancy in code and difficulty in scaling.

The benefits of using MVP are -

That’s a simple activity without MVP!
With MVP
  • Easy Data Modelling: It is always a good practice to separate the data layer from the views of the app. The data can then be modelled separately in a different layer to cater to multiple views. This is where the presentation layer comes in handy.
  • Easy Debugging: By creating three different layers of abstraction it becomes easier to test and debug each layer in isolation and narrow the source of a bug down to one of the three layers.
  • Increased Code Re-usability: The developer has to write a lot less code because a single data source serves all the views. This saves bandwidth since similar data doesn’t have to be queried more than once.
  • Easily testable code: As the business logic is separated from the UI logic, writing test cases becomes much more simpler and the test coverage can increase significantly.
Look how excited Shakira is about MVP!

An Example of MVP in Prowl

An interface is first created which houses the abstract methods that the View and Presenter must implement. This helps us decouple the code from the implementation and helps us abstract the framework.

A thumb-rule while implementing the MVP pattern is to make your View as dumb as possible. All the view does is handle UI changes, everything else happens inside the Presenter.

The presenter is incharge of modifying the view while it fetches data and then update the view once it is done.

Conclusion

Adopting a MVP pattern in an android app makes life much more easier for us developers. It stays true to the adage, ‘If in doubt, keep it simple silly’. It makes our code much more organised, segmented and by implication, easier to debug and write tests.

Give MVP a try and spread the word!

If you’re interested in working with some of the best technology and business talent in the country? Shoot out an email to us at careers@treebohotels.com. We love talking to and meeting awesome Engineers and Product managers.
Follow us on Medium to keep a track of some interesting insights from our engineering team on various fronts!