MVVM
, short for Model-View-ViewModel
, is an improvement based on the MVC
and MVP
architecture patterns. In MVVM
, the status and behavior of the View
in the MVC
pattern are abstracted, separating the visual UI
from the business logic, more clearly distinguishing the development of the user interface UI
from the development of the business logic and behavior in the application. The MVP
pattern does not belong to the category of the generally defined 23
design patterns, but is usually considered a broadly architectural design pattern.
MVVM
simplifies the dependency between the interface and the business, helping to separate the development of the graphical user interface from the development of business logic or data models. In MVVM
, the ViewModel
acts as a binder to link the view layer UI
with the data layer Model
. When the Model
is updated, the ViewModel
updates the data to the View
through the binder. When the View
triggers a command, it passes the message to the Model
through the ViewModel
. The ViewModel
acts like a black box, in the development process, only needing to focus on presenting the view layer of UI
and the data layer of the abstract model Model
, without needing to pay too much attention to how the ViewModel
passes data and messages. The MVVM
pattern behaves similarly to the MVP
pattern, but its main difference is that it usually adopts two-way binding data-binding
, automating the synchronization logic between the View
and Model
. Previously, the Presenter
was responsible for manually synchronizing the View
and Model
, but now this is handled by the framework's data binding functionality, only needing to inform it which part of the Model
corresponds to the data displayed by the View
.
Model
can also be referred to as the data layer, focusing solely on the data itself, without concern for any behavior.View
is the structure, layout, and appearance that the user sees on the screen, i.e., the visual UI
.Model
is updated, the ViewModel
updates the View
through data binding.ViewModel
abstracts the view by exposing public properties and commands.ViewModel
communicates between the view and the data binder.Model
is updated, the ViewModel
updates the data to the View
, and when the View
triggers a command, the ViewModel
passes the message to the Model
.View
can change independently of the Model
, a ViewModel
can be bound to different View
s, so when the View
changes, the Model
can remain unchanged, and when the Model
changes, the View
can also remain unchanged.ViewModel
, allowing many View
s to reuse this view logic.Model
, while designers can focus on page design.ViewModel
.Bugs
difficult to debug. When the interface is abnormal, it may be due to a problem in the View
code, or it may be due to a problem in the Model
code. Data binding makes it possible for a Bug
in one place to be quickly transmitted to another place, making it less easy to locate the original problematic area.Below is a simple data binding example implemented according to Vue
. However, it is worth noting that Vue
does not fully adhere to the MVVM
model, but rather takes inspiration from it. See https://cn.vuejs.org/v2/guide/instance.html
for more information. To understand why Evan You says Vue
does not fully comply with MVVM
, you can refer to this https://www.zhihu.com/question/327050991
.