Do you know what an Architectural Pattern is ?
It is basically a reusable solution to a commonly occurring problem in software architecture within a given context. It addresses a variety of issues in the software engineering field. Developers generally use different patterns and tools that help them with the development and testing for various websites and applications. Also, we need something that allows faster reaction to design changes, which MVVM actually does. MVVM is a software design pattern, separating an application to three main logical components.
Note: Link between Model and View Model is Manipulating Data and between ViewModel and View is 2-way Data Binding.
View tells the View Model that the user has performed some action and based on that, View Model tells the View how it should update itself.
Example : User pulled the table view and pull to refresh was implemented.
So, the view will now tell the View Model that the user has pulled the table view and hence View Model knows that the service call has to be made, and now View Model will interact with the Network Layer. Network Layer will make the service call, models will be updated with new data. When the Model will be updated, View Model will let the View know that data is available, update yourself.
DESCRIPTION OF THE MODEL :
MODEL: ( Reusable Code – DATA ) Business Objects that encapsulate data and behavior of application domain, Simply hold the data.
VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEW-MODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.
- Life Cycle state of Application will be maintained.
- Application will be in the same position as where the user left it.
- UI Components are kept away from Business Logic.
- Business Logic is kept away from Database operations.
- Easy to understand and read.
BASIC EXAMPLE: We want to display Name in Pink Color (not written in proper format, proper length) or Display Pink Color if Age of a person is > 20 years, Display Red Color if Age of a person is < 20 years, Then the Logic of Red and Pink Color would be present in ViewModel.
DIFFERENCES BETWEEN MVVM AND MVC:
- The Model is somewhat similar to MVC but here we have ViewModels which are passed to the view and all the logic is in the ViewModel and hence no controller is there. Example: Knockout.js
- MVVM is relatively a new Model.
- We are on the client side so we can hold on to objects and do a lot more logic in a non disconnected state
- Debugging process will be complicated when we have complex data bindings.
- MVVM would be easier for separate unit testing and code is event-driven.
- In this pattern, we have models which are basic objects with no code and just properties, View’s contribute to presentation items (HTML, WinForms, etc) and Controllers focus on the logic part. Examples: ASP.NET MVC, Angular
- MVC is an old Model.
- MVC is typically used when things are transactional and disconnected as is the case with server side web. In ASP MVC we send the view through the wire and then the transaction with the client is over.
- MVC is a bit difficult to read, change, unit test and to reuse as well.
- Model can be separately tested from the user.
- Maintainability – Can remain agile and keep releasing successive versions quickly. 2. Extensibility – Have the ability to replace or add new pieces of code.
- Testability – Easier to write unit tests against a core logic.
- Transparent Communication – The view model provides a transparent interface to the view controller, which it uses to populate the view layer and interact with the model layer, which results in a transparent communication between the layers of your application.
- Some people think that for simple UIs, MVVM can be overkill.
- In bigger cases, it can be hard to design the ViewModel.
- Debugging would be a bit difficult when we have complex data bindings.
SUMMARY: From Server, Get Data(available in Model Objects), View Model reads Model Objects, and then facilitates easy presentation of data on the view.