Sergey_Nivens from iStockPhoto

Clean Architecture vs MVVM

Andrey Solera
3 min readDec 17, 2020

--

This article will focus its content on comparing a Clean Architecture and Model View View Model (MVVM), both structures being widely used in programming and each of them have their own advantages and disadvantages.

Each one of these architectures are and must be agnostic of the IO device on which the software will end up in.

Clean Architecture Overview

The main idea is to extract the outer layers so that inner layers can be separated and protected from external changes (made on those outer layers).

These outer layers use instances of the inner layers as shown in the image above, however, the inner layers don't have dependencies of the outer layers. The previous statement satisfies the Dependency Inversion, one of the principles of the SOLID acronym.

As we can see in the image below, this architecture can be separated into 3 main layers:

Domain Layer

As shown in the image, this layer contains the Entities, Use Cases, and Repository (only the interface).

Data Layer

Contains the Repository (it's implementation), Database, and the APIs used by the software.

Presentation Layer

Contains the Presenter, Controller, and View.

Advantages

  • Creates a clean (pun intended), way of separating parts of the code in a way that if modified/changed/removed/added, they won't change nor affect the other parts of the code.
  • IO agnostic, which means that it doesn't matter in which program/io device will end up, only (in my humble opinion, the UI is going to be majorly affected.
  • Very easy to test.

Disadvantages

  • Creates a lot more code than usual. This can be easily deduced because instead of accessing a certain part of the code directly, we have to modularize it in order to maintain separations between different components.

MVVM Overview

The main idea of this architecture is to separate the code into 3 major components. MVVM is a derived product of the MVC and MVP architectures, which makes sense (as you'll see later) since the ViewModel acts as a mix of the decision making part of the code and also the one that exposes data to another component (usually the View).

The 3 components mentioned before are:

Model

This is basically the domain model, which are the objects (POJOs) that contains information of the core objects that the software deals with, i.e In a booking software system some of these objects are:

  • Reservation
  • User (which owns each reservation)

among other objects that form the code of the software.

View

This is basically the UI part of the software, i.e In an Android application, the views will be the activities/fragments/each view object that will listen to data in order to be displayed to the user. Notice the word listen, this is because a proper MVVM architecture decouples the View from the View Model. This decoupling can be obtained by using the Observer pattern.

View Model (our controller/presenter)

Remember that the point of a controller (in the Clean Architecture) has the functionality of receiving all kinds of input of the data in order to be processed by a/several use case(s). While the Presenter is in charge of handling the information that is passed from the UseCases and send them into the View.

For MVVM architecture the View Model takes both responsibilities.

Advantages

  • Easy to implement, we only need to handle 3 major components.
  • Easy to test.

Disadvantages

  • Doesn't separate concerns as much as it should.
  • Since the View Model acts as the Controller and the Presenter, it can get very big, which in itself is a code-smell. Classes shouldn't be extensive, since this indicates that we're doing something wrong.

In my opinion, MVVM is a good architecture; however, the Clean Architecture makes a lot clearer separations between each component.

Programmers should really analyze the software that is going to be written and the extension and difficulty that each architecture comes with.

--

--

Andrey Solera
0 Followers

I am a Costa Rican developer with over 10 years of experience building mobile applications with a deep passion for software.