- MVC vs MVP vs MVVM
- Trung Anh Dang
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: snowyYU
- Proofreader: Onlinelei
MVC, MVP, MVVM comparison
There are so many design patterns to choose from. After developing some apps using the model-View-ViewModel (MVVM), Model-View-Presenter (MVP), and Model-View-Controller (MVC) patterns, I think it’s time to talk about the differences. To make it easier to understand, here’s an example: developing a little feature to find books, which is actually the search feature in those BookSearch apps.
Let’s get started
MV(X) basic concepts
Before delving into the MVC, MVP, and MVVM architectures, let’s take a quick look at them.
Why was model-view -(C or P or VM) born?
The original purpose of each of these architectures was to break up applications with graphical interfaces into different parts: views, handlers, and data management.
There are more goals now
- modular
- flexibility
- measurability
- maintainability
Model-View-Controller
MVC, for short, is a widely used design pattern for architecting software applications. The pattern was originally designed by Trygve Reenskaug while working on Smalltalk-80 (1979) and was originally called model-View-Controller-Editor. MVC was described in more depth in the 1994 book “Design Patterns: The Foundations of Reusable Object-oriented Software” (aka “GoF”), which played a role in popularizing its use. This pattern divides the application into three parts.
- Model — Responsible for the application’s business logic. It manages the state of the application. This also includes reading and writing data, persisting application state, and may even include tasks related to data management, such as networking and data validation.
- View – This section has two important tasks: presenting data to the user and handling user/application interactions.
- Controller — The View layer and the Model layer are bound together via one or more controllers.
The Model – View – Presenter
MVP is a derivative of the MVC design pattern, which focuses on improving presentation logic. It originated with a company called Taligent in the early 1990s, when they were developing a model to run on a C ++ CommonPoint environment.
Although MVPS are derivatives of MVC, they are also slightly different.
- Model — Model represents a set of classes that describe business logic and data. It sets rules for changing and manipulating data.
- View — The View is responsible for interacting with the user, as shown in the XML, Activity, and fragments sections below. It is independent of the logic to be implemented in the process.
- Presenter — The Presenter takes input from the View, processes the data with the help of model, and passes the results back to the View when it’s done.
Model-View-ViewModel
MVVM was originally proposed by Microsoft for Windows Presentation Foundation (WPF) and Silverlight, It was formally introduced by John Grossman in a blog post about Avalon (code name for WPF) in 2005. This MVC – and MVP-based pattern aims to separate the development of the UI in an application from the development of the business logic.
It consists of the following three parts.
- Model – The Model layer in MVVM is very similar to the Model layer in MVC, containing the basic data needed for the provisioning to function properly.
- View – The View layer is the graphical interface between the user and the design pattern, similar to the View layer in MVC. Used to display the processed data.
- View-model — The view-model is both an abstraction of the View layer and provides a wrapper around the Model data to be accessed. That is, it contains a model that can be transformed into a view, and it also contains commands that the view layer can use to change the model.
MVC vs MVP vs MVVM
Let’s look at the major differences between MVC, MVP, and MVVM.
Performance evaluation – When we tested the performance of the UI, in terms of rendering frames, MVP was considered the most reliable and least blocked. Data binding in MVVM creates additional overloads, which can lead to serious performance degradation when performing complex tasks.
Compatibility – When testing their compatibility, MVVM is the best of the bunch, thanks to its data binding. MVP performed better than MVC, which had serious reboot issues.
Extensibility — When it comes to design patterns, they have to be extensibility so that we can constantly add new features and strategies to our applications.
- Based on the above points, the DIFFERENCE between MVP and MVVM is smaller, but MVVM is more maintainable.
- In most cases, an APPLICATION using the MVC pattern needs to change more with each subsequent extension.
References – In MVC, a view does not directly refer to a Controller, whereas in MVP, a view refers to a Presenter, and in MVVM, a view refers to a View-Model.
Application entry – For MVC, the application entry is Controller, while for MVP and MVVM, the entry is View.
Use different architectures to accomplish the examples above
Try developing a BookSearch application using one of the following architectures to help you understand the above.
Easy, right? You can try it yourself now.
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.