MVVM Design Pattern in Android Development

Betül Kırçıl
4 min readSep 14, 2023

--

In this article, I would like to talk about the MVVM design pattern, which is the most widely used in Android application development. But first, I want to answer the questions like “Why was it needed, should it be used?”

The most common problems when developing software projects are maintainability, testability, slow development speed and complexity. Unless the necessary are met, codes we write become unreadable, unusable. In situations like this, when a developer is added your development team, adapting can be difficult and take time.
That’s why I would like to talk about the SoC (Separation of Concerns) principle, which we must use in every project, and what it does for us in practice.

SoC:

SoC (Separation of Concerns) is the independent development of code blocks that perform different functions by separating them from each other. In other words, we need to divide our codes into as many parts as possible according to their functions. In this way, we increase cohesion by reducing coupling in our codes. In other words, while each module is developed independently of each other, it also applies the single responsibility principle.

Benefit of SoC (Separation of Concerns):
* Provides flexibility for projects. Since each module is developed independently of each other, any change or addition only affects the module in which it is made.

  • It facilitates product removal by keeping coupling to a minimum and cohesion to a maximum.
  • It provides high code quality thanks to codes developed independently of each other.
  • Provides easy testability.
  • Prevents us from writing complex codes.
  • Bugs can be fixed easily.
  • Rapid development possible.
  • It provides codes with high readability and maintenability.
  • In case of a possible problem, other modules or layers will be affected in a limited way.

After briefly touching on SoC, we can move on to MVVM.

MVVM:
The reason why I explain SoC before explaining MVVM; The basis of the design patterns like MVVM, MVP etc. was actually SoC. We only use this principle by customizing it according to our projects. Thus, different design patterns emerge. MVVM is one of those design patterns and is used quite frequently.

MVVM is a design pattern that minimizes independence and increases modularity by separating the user interface and back-end operations. It also allows us to configure code optimization.

MVVM design pattern has 3 layers:
1- Model: It is the layer responsible for abstracting data while working with web services, databases or different data sources. It works with the viewmodel layer to fetch and save data.
2- View: This part is the layer containing the codes with which the user will interact. Business logic is not processed here. In this layer, the user’s actions on the application are generally reported to the viewmodel and the data from the viewmodel is observed. Thus, transactions are carried out between the user and the screen.
3- Viewmodel: Establishes communication between the View and the model layer. It ensures the preparation of user interface units that will be presented to the user when a request is made from the view as the logic operations are performed.

Location of MVVM within the application:
We call the relevant method from the viewmodel to implement business logic based on the user action. According to the logic in this method, data on local or remote servers is accessed. Then, in the opposite direction, the mutableLiveData value in the viewmodel is changed, the changes are followed with the observer in the view and the interface is updated.

Benefits of MVVM:
* Provides modularity. Code blocks that serve different purposes are grouped.
* It allows us to configure code optimization.
* Provides flexibility.
* It allows us to develop code in a layered structure.
* When changes are made, project development becomes easier by making changes only in the relevant layer. Because every job is done in its own layer, it is not interested in the work of other layers.
* Comprehensibility and readability increases, making adaptation easier for developers who join the team later.

--

--

Betül Kırçıl

Computer engineering student at University of Bakırçay