Often the question is: “Should I use MVVM or MVC design pattern?” The answer depends on the application platform. MVC and MVVM both provide separation of concern. But the concerns are separated to greater extent in MVVM.
For e.g. in Web applications, if the view (or presentation) layer lacks substantial data-binding abilities or user workflow decisions need to be done at the backend, MVC design pattern would suit better.
However, in the case of mobile and desktop apps, it may be worth considering MVVM as it provides the following advantages for these platforms:
- Reduced Code size – Data Binding framework handles the functionality to display the data and to automatically update the view to reflect any changes in the data. This effectively reduces the code size and maintenance costs for the code.
- Easier to test – View Model is connected to the view through data binding. So there is a clearer separation of concern. As view model is not dependent on the view, it has less dependency in testing.
- Sharing across platforms – When UI elements are common, the same view model can be used across multiple platforms. This will save duplicate efforts in both development and testing.
- Workflow independent of the view – View Model can use command pattern and handle workflow independent of the view. So it can be shared across platforms. Also, since there is no dependency on the view, it is easier to unit/test it.
- Smaller footprint – Since View Models are light weight, they have smaller memory footprint.
- Parallel Development – Developers and designers of the view can work independently.
- Containment of the changes –
- If changes are made to the service layer, it is contained in that layer itself.
- If changes can be made to model, it will have less impact as it does not directly impact the view.
MVVM can have the initial cost of creating both the Model and View. But looking at the benefits it would provide in the long term, it is worth the cost.