DEV Community

Cover image for MVVM explained
Sinazo Bogicevic
Sinazo Bogicevic

Posted on

MVVM explained

Model View ViewModel or MVVM isn't as hard to understand as you might think. It really just describes one way we can write and organise our code so it's easier to maintain, refactor and test.

MVVM basically says look instead of writing all of your code inside your fragment or activity, separate it into three distinct parts, the View, ViewModel and Model.

Alt Text

  • View that where you're going to place your business logic and it's going to provide the data to your UI.

  • ViewModel is going to act as a bridge between the View and Model. ViewModel provides data to the view and it communicates with the model.

  • Model contains the business logic that handles data operations i.e querying local and or remote data source.

Each parent component is only dependent on the child component directly below it. The child component doesn't know about the parent above it. This means that if anything happens to the parent component, the child is not affected by that change. The child components may not have a direct reference to the parent component above but it does have a reference to the parent by Observables. The child exposes the Observable to the parent component so that the parent can listen and respond to any changes.

As you go along your coding journey you'll definitely come across other buzzwords like MVP or MVC these are just other design patterns. There is no rule set in stone regarding which pattern you should use however it is good practice to follow best practice and to separate your code into distinct sections.

Top comments (0)