Posted on: 25/05/2018
This post presents the concepts behind MVVM pattern and an illustration of how it actually works using a mini ReactJS demo.
What is M-V-VM pattern?
M-V-VM stands for Model-View-ViewModel, which is a software architectural design pattern invented by Microsoft. The purpose of using MVVM is to separate the design of User Interfaces (UI) to the development of the business logic (or the back-end data models). By such separation, the developers of different teams can work freely on their own stuffs.
For example, the UX team can design, implement and customize the UI on the front-end to whatever they want while the Dev team can develop the codes on the back-end without worrying about the UI, and in the mean time, the Testing team can execute the tests on the completed stuffs on both ends at the same time. The only thing that ties those teams together is that they need to know what are the data being sent from the Model to the View, and how it is formatted. Here comes the ViewModel. Then what exactly means by MVVM?
JSON
or XML
(the most common formats). The ViewModel handles data changes on client-side and updates the View in real-time manner. All the changes in data will then be sent to server-side to update the Model. The communication between Model and ViewModel is done via AJAX calls, which send requests to the server-side to invoke some method and get back the server response.The below diagrams demonstrate the basis of MVVM pattern and a simple Client-Server communication.
In the early time, MVVM was integrated mainly in some .NET systems, however, it is being utilised recently and developed by many client-side Javascript Frameworks. Some of the most popular Javascript frameworks include ReactJS, Angular JS, Vue.JS, KnockOut.JS and EmberJS.
What is the difference between M-V-VM and M-V-C?
M-V-C is a short-hand denotion of Model-View-Controller. MVC is also a tructural design pattern that was first introduced in the 1970's and has then been emerging since the 1980's. There are, nowadays, a lot of variants of MVC pattern such as Model-View-Adapter (M-V-A), Model-View-Presenter (M-V-P) and M-V-VM, etc. MVC allows the development processes to be simultaneous (eg. different people can work on different features at the same time), and helps increase the reusability, maintainability and modifiability of the source code. In order to achieve such advantages, MVC aims to increase the cohesion (the methods of a certain controllers are effectively grouped together, no logics of this controller can be found in another one), and decrease the coupling of the components (each component should be designed to handle its own tasks and only dependent on other components as fewer as possible).
Despite of many benefits that MVC can offer, there are some disadvantages and the most noticeable one is Code navigatability. In fact, it would be hard, even impossible, to switch to another framework or programming language once the development process has started. Even updating to the newer version of the same programming language or framework could also be a headache to the developers if the framework does not properly support updating. Other factors could be: (1) developers need to be expertise in multiple technologies to work in MVC, and (2) developers should regularly refactor the source code, which is contributed by many people, to maintain the level of cohesion and coupling.
So, we can see MVC and MVVM are having the M (model) and the V (view) in common. Basicly the MVVM is an improved variant of the MVC to serve in some specific purposes of software design and architecture. The difference between the Controller and the ViewModel, can be understood as:
The model data and ViewModel are required to be formated as closely as possible, so that the confusions between different development teams can be lowered and communication is easy. Other than that, there are some other advantages of MVVM:
M-V-VM disadvantages
While some people may argue that they can create a website with those real-time updates without the aid of MVVM, just using Javascript, JQuery and AJAX. Well, they may also need think of how much of efforts and time they will need to invest in the project for implementation, testing, modification and maintaining, how good and stunning the features would be, and how hard for themselves or someone to get their codes after a time of leaving. Therefore with M-V-VM design pattern, they can not only make their codes clean, bug-free and maintainable, but they can also enable the project to be extendable with ease in a time later. Although the advantages are undeniable, MVVM also has some implicit disadvantages:
This post reflects my researches about MVVM. To find out more, please click on YUGIOH Mini Deck Demo to take a look at MVVM as a single-page application.