MVI(Model-View-Intent) Pattern in Android

Shubham Soni
Code Yoga
Published in
3 min readMar 16, 2018

As an Android Developer we come to hear these words MVC,MVP,MVVM daily and we have to decide which architectural pattern we have to use in our app.Today we are going to give a quick look on a architectural pattern which is not so popular in android world but have good grip on the Javascript Community.MVI is introduced by Cycle.js(JavaScript FrameWork) by André Staltz and it is also used in other popular javascript Frameworks like Redux.

In Android the MVI Pattern is adopted by Android enthusiast and open source lover Hannes Dorfmann. So,lets go and look what the heck MVI pattern looks like!

Model-View-Intent

The major GUI architectures, MVI is defined fairly loosely. However, at its core, it is a return to the unidirectional data flow that MVC offered… though with few more parts:”

  • Intent-Intent is not Intent. The intent simply describes an intention, or action, or command generated by the user as he or she is interacting with the app.An intent represents the interaction with the view by the user. The intent delivers changes that need to be processed by the Model.
  • View- The view, actual the UI, observes and depicts the model’s state. The view forwards each interaction as intent.
  • Model- Model represents a state (data to be displayed, visibility of your View widgets, RecyclerView scroll position, etc.).This includes interactions with business logic. The business logic updates the current state and thereby the observing View.

If you going going to implement MVI (Model-View-Intent) in your app then instead of starting from scratch you can use mosby library developed by Hannes Dorfmann. Ofcourse it is not mandatory that you should start with a third party library in order yo implement MVI pattern in your app.One advantage of using mosby library is that you get the base classes on which you can build you app.

Differences between MVI and MVP

Model-View-Presenter (MVP) is a software architectural pattern which is based on Model-View-Controller. It is used to implement user interfaces while keeping the model seperated from the view. The interactions are controlled by the presenter which acts as an mediator between model and View.

Model-View-Intent (MVI) is also a derivation of the original MVC architectural pattern. The pattern fulfills the essential purpose of the original MVC as dicussed by André Staltz in his description of MVI. Instead of working with a proactive controller MVI works with the reactive component called intent.

Model-View-Presenter:

  • Seperation between model and view. The presenter acts like a mediator.
  • One to one mapping between View and Presenter, with the possibility to use multiple Presenters for more complex Views

Model-View-Intent:

  • Unidirectional Flow allows us to work without callbacks between the View and in this case the Intent
  • Compulsary Usage of RxJava
  • Immutable objects

What should you use!

Its completely up to you as you choice could be different according to your need and as MVP is more popular and has a good amount of documentation and tutorials online its easy to work with MVP and in the case of MVI it still don’t have a good documentation and lots of tutorials like MVP But If you are going with MVI the start with Mosby MVI as it offers the benefit that RxJava is already established in its library calls and a loose coupling between UI and business logic is inflicted.

--

--

Shubham Soni
Code Yoga

Senior Executive @Ketto @Dart @Flutter @Java @Android, Editor @FlutterCommunity @CodeBurst.io, App Developer @Senior @Moderator @FlutterDeveloper