There is no one-size-fits-all approach when developing apps, and most certainly not when developing Android apps. Different architectures have their perks, and although some might be more suitable for certain tasks, it’s important to understand the differences before choosing one over the other.
One of the most fundamental differences between these three architectures has to do with how the UI is handled. In the Model-View-ViewModel (MVVM) architecture, the Model is responsible for managing the data and passing it to the View, while the View is simply there to produce an experience that the user can interact with. The ViewModel is responsible for mediating the changes that occur between the View and the Model, such as when a button is pressed or a list is scrolled.
The other two architectures, Model-View-Controller (MVC) and Presentation-View-Model (PVVM) are very similar to the MVVM approach, but instead of using a ViewModel, they directly link the View to the Model. This approach was popularized by IOS and is the default architecture for most mobile apps.
What are the differences between these three architectures? Let’s dive into it.
One of the defining features of the MVVM architecture is it breaks the traditional link between the UI and the business logic, which gives it a very clear separation of concerns. This allows independent teams to work on the UI and the business logic, which can be quite beneficial.
The developer working on the UI does not need to worry about the business logic and vice versa. This is a lot of overhead that could be eliminated if both parties are independent. The only overhead that might arise is from trying to figure out how to integrate the two components – UI and business logic –. But that’s a problem that any architecture faces, and it’s not one particular to MVVM.
One more thing about the MVVM architecture that is particularly beneficial is it separates the UI into its distinct layer, making the code more maintainable and debuggable. This debuggability is critical as UIs are often the source of the most bugs in complex apps, and it’s essential that they can be easily isolated and broken down into their constituent parts.
The Model-View-Controller (MVC) architecture, which was originally designed for desktop apps and web servers, does not have the same clear demarcation between the UI and the Model as the MVVM approach. This is in part because the Model and the View are tightly coupled in the MVC approach.
The UI and the Model are still separate components, and it’s the job of the developer working on the UI to decide what data should be presented to the user and how it should be presented. It is also the job of the developer working on the Model to decide what calculations should be done on this data and how these calculations should be presented to the developer working on the UI.
The MVC architecture is responsible for tying the UI to the Model, so whenever the Model changes, the View will automatically reflect these changes. The developer working on the UI is responsible for listening for changes to the Model and calling methods on the View to cause those changes to be reflected in the UI. In other words, the UI and the Model are tightly coupled in the MVC approach.
Another key difference between the MVC and the MVVM approach is the MVVM approach has one clear and independent UI component, while the MVC approach has three components (the Model, the View, and the Controller). Although the UI component in the MVC approach can be separated from the Model, it still needs to coordinate changes with the View (this is where the View’s role as a mediator between the UI and the Model comes in). In short, if you want a highly reusable and easily updatable UI, the MVVM approach is what you should go for.
The Presentation-View-Model (PVVM) architecture mirrors the MVC approach, with a clear demarcation between the Presentation layer, which is responsible for the visual presentation of the data, and the Model and the View, which are responsible for managing the data and its processing, respectively. In the PVVM approach, the View is still tightly coupled to the Model, but the Presentation component is not. This allows independent teams to work on the UI and the Presentation layer together.
The primary differences between the PVVM and the MVC approaches have to do with how the UI is handled. In the MVC approach, the UI is an extension of the Model, so any changes to the Model will immediately be reflected in the UI. On the other hand, in the PVVM approach, the UI is not directly tied to the Model, which means that changes to the UI do not necessarily mean the Model has changed. This is critical if you want the UI to remain stable over time as changes are made to the Model.
One more thing about the PVVM approach is it allows the UI to be easily testable, which can be quite beneficial as it allows independent teams to work on the UI and the Test component together (usually a QA team). This collaboration results in a much more efficient testing process and higher-quality code.
Now that you have an idea of the differences between these three architectures, let’s discuss which one is the best for your needs.
MVC, MVP, and MVVM are all popular architectures for Android development, but they each have their strengths and weaknesses. The MVVM architecture is beneficial for clear separation of concerns and maintainable, debuggable code, but might result in integration overhead. The MVC architecture is tightly coupled between the UI and the Model, while the PVVM architecture has a clear demarcation between the Presentation and the Model, resulting in a more testable UI. It’s important to understand the differences between these architectures before choosing one for your project.