How to implement VIPER aka Clean Architecture on Android
VIPER LAYER STRUCTURE
● 5 organized layers
● Single responsibility
● Easy to read
● Mid to huge projects
Should take care of following:
● UI rendering
● UI components
● Data displaying
● Should wrap main View objects and provide them to lower levels
● Android API specific stuff which does require Activity Context
This layer should behave like a bridge between UI and it’s data.
● Handle UI events
● Update UI based on events
● Communicate with ViewModel to update / gather data
● Should use Data Binding framework
This layer should hold View state and data.
It should take care of:
● View state operations
● Persist over UI configuration changes
● View related data operations
● Should use Observer pattern to notify changes
This layer should operate with all the business logic data necessary for the ViewModel
and should push them there.
Should take care of:
● Data operation to / from various data sources
● ViewModel-ready data preparation
Basically an POJO object which should describe business logic related objects.
Basically an utilized class to handle screen related navigation. Therefore any Fragment
or Activity starting / showing / hiding operations.
Similar to Navigator pattern.
Why should I choose VIPER architecture for my project?
● Good modularization
● Concern separation
● SOLID principles
● Clean to read and understand
● High testing ability (good for TDD)
“It is not enough for code to work.“
ROBERT C. MARTIN,
Clean Code: A Handbook of Agile Software Craftsmanship
Differences against MVVM
● More abstraction
● Best for more complex projects
● Overkill for simple applications
● More layers thus better concern separation
● Better testability
- It is really complex to implement it.
- You should be patient and careful
when building the base.
- If you are going for a huge project.
=> Don’t reinvent the wheel.
Interactor and ViewModel
● JUnit || Roboelectric (unit tests)
View and Presenter
● Espresso (UI tests)
Simple Weather App Example
YOU WILL LOVE IT... ONCE YOU UNDERSTAND
○ Android VIPER architecture
○ Data binding