cool hit counter An introduction to the advantages and disadvantages of the MVVM design pattern_Intefrankly

An introduction to the advantages and disadvantages of the MVVM design pattern

First of all, let's talk about the origin of MVVM. I believe that you are no stranger to MVC, which is the most classic design pattern for developing clients, but there are serious problems with MVC that people can't ignore. In normal development, all parts of the development, except for the simple Model and View, are placed inside the Controller. The Controller is responsible for displaying the interface, responding to user actions, network requests, and interacting with the Model. As the business logic increases, the controller's processing logic becomes more and more complex, and the controller slowly becomes fatter and fatter, which results in complex Controller logic that is difficult to maintain. In order to manage the code better and scale the business more easily, it was necessary to slim down the controller, so MVVM was born.

MVVM: is Model-View-ViewModel shorthand

M:M corresponding to the MVC

V:V corresponding to the MVC

VM:viewModel, is to separate out the data loading and processing functions of the controller in MVC

MVVM be Web A very popular development pattern on the front end, take advantage of MVVM can make our code more focused on handling business logic rather than caring about DOM operations。 current famous MVVM The framework has vue, avalon, angular etc., Each of these frameworks has its own merits, But the idea behind the implementation is largely the same: Data binding harmony View Refresh。 withMVC the same as, The main purpose is to separate the view(View) and models(Model)。View Can be independent ofModel Changes and modifications, anViewModel can be bound to a different"View" upper, properView The time of changeModel Can be unchanged, properModel The time of changeView It can also remain the same。

In MVVM, data is the core. Due to the two-way binding between VIewModel and View, the data in the ViewModel is manipulated and synchronized to the DOM, and we monitor the user's changes to the DOM through DOM events, which are also synchronized to the ViewModel.

Benefits of MVVM design pattern

1.Two-way binding technique, when the Model changes, the View-Model is automatically updated and the View is automatically changed. It's good to have consistency in the data, don't worry, in one piece of the module the data is this value, in another piece it's another value. That's why the MVVM pattern is sometimes called: the model-view-binder pattern.

2.The View is further enhanced with some of the functionality of the control, and if you want to enhance it infinitely, even almost all of the controller's functionality can be migrated to individual Views (although this is not desirable, then the View does something that is not part of its responsibility). A View can have its own View-Model just like a controller.

3.The controller is greatly slimmed down as most of the controller's functions are moved to the View to be handled. No more fretting about seeing huge controller logic.

4.A function can be abstracted from the data processing part of the View or ViewController to handle the mod. This way they specialize in page layout and page jumps, and they are necessarily a step more simplified.

Disadvantages of MVVM design pattern

1.Data binding makes bugs hard to debug. If you see an interface exception, it could be a bug in your View's code or a problem with the Model's code. Data binding makes it less easy to locate the original problem when a bug in one location is quickly passed to another location.

2.A large module in the mod will also be very large, although the use of easy to use is also easy to ensure the consistency of the data, when held for a long time, not to free memory caused to spend more memory.

3.Two-way data binding is not conducive to code reuse. The most common reuse for client-side development is View, but the data bi-directional binding technique allows you to bind a mod to a View all in one, with different mods for different modules. Then you can't simply reuse the View.

This article was written by

Source Code Era H5 Front End Discipline

Lecturer Original!

1、Macro College holds the second academic lecture for the university celebration
2、Zhejiang to build superhighway with full support for autonomous driving
3、Today at 0152 AM the BCH public chain officially completed its hard fork
4、Robot Programming Series Captain Dragonfly
5、Five Cybersecurity Predictions for 2018

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送