In today’s programming world it’s important to write code in a standard way. For that, we are studying different types of architectural models. Out of that MVC and MVVM are mostly used design patterns and everybody has a good idea about MVC. But when it comes to MVVM developers are not able to differentiate it properly. So I am trying to explain it simply with the same example for both Architectures so that we can understand it properly.
Before understanding the MVC and MVVM I want to add an example which we are going to use.
We are going to show the todo list of users in the table view and going to show the status of the todo task which is completed or not. For completed tasks we are showing in green color and for not uncompleted tasks showing the yellow color.
This is how we are getting the response from API
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
Everybody knows MVC stands for Model-View-Controller. Let’s see in detail.
Model is nothing but it holds all the properties of the particular object i.e it holds all the data of your app. The work of Model is only providing the data to the required fields not doing any type of operation on it.
By considering the above problem statement, the following is the model of our app.
This is the face of your app. Means this class does not contain domain-specific logic. In our case, Storyboard and cell is our view of the class where we are showing the data.
In the above code snippet, you can see that it’s a cell class which is responsible for showing the data over the cell, and because of that this class is said to be the view.
The controller is a way of doing communication between model and view. So basically the class which is responsible for doing the communication between View and model is called a controller.
In the above example, you are seeing the controller is responsible for fetching the todo list data and getting it in form of the model and then the model is assigned to the view/cell in form of a function so that data can be shown.
This is how MVC architecture looks like. Now we will move forward towards MVVM taking consideration the same example
MVVM Stands for Model-View-ViewModel. Now similarities between MVC and MVVM are only models. View and controller perform different duties as compared to MVC. The model doesn’t have direct communication with View and Controller. What is the meaning of not direct communication with View and controller? It means View and Controller don’t have any instance of the model in their class.
Also In MVC, you can see there is a logical part of the cell. But in MVVM there is not any type of logical code as it only assigns data to the view. Let’s see in Detail:
The model is the same as what we discussed in MVC. So I am not going to discuss it here again.
In MVVM View is also the face of your app, the only difference is a load of views is distributed and no logical part is included in View. It only gets data from the view model and shows the data. And the view doesn’t have direct communication with the model. So how does View get data? So the view is communicating with ViewModel for data. Let’s see the code snippets so you can get the exact idea about it.
Here you can see the function showData is commented and also there is no instance of todomodel1 in code instead you can see the todoViewModel. Also, the logical part where we are showing the status completion is also not there. So this is the major difference between MVC and MVVM View. Here you can see the ViewModel only returns the color, title, and status type. It’s none of the view’s duty to check for the status. It only shows what ViewModel sending.
This is the new thing added in MVVM instead of the Controller. It doesn’t mean the controller is not playing any role here. The controller is playing the same role that it was in MVC. The only difference I already told is it doesn’t communicate with the model directly instead it communicates with the View-model. View model performs all the logical work and relieves the controller and view doing this work. So that they can only focus on their major work of representation of data.
Let’s check what ViewModel is doing the job here.
You can see ViewModel getting an instance of model class and doing all logical work of checking status and assigning color. View models hold all the information and logical operation in ViewModel.
In this case, we are unable to find out what the controller is doing. So basically the controller is doing all the work which it was doing in MVC only difference is the controller doesn’t know about the model it holds the ViewModel only. Let’s check the following code snippets:
In the above code, you can’t see any instance of Model here. As in the MVVM, the view controller doesn’t communicate with the model directly. Here you can see the ViewModel instance which we get. And we are directly assigning it to Cell.
So in MVVM, the role of View and controller is only assigning the value not calculating the value.
You can find code for MVVM and MVC by using the following link.
MVVM – https://github.com/iYuvraj-kale/MVC_to_MVVM.git
MVC – https://github.com/iYuvraj-kale/MVCDemo.git
The difference between the MVC and MVVM is View and controller are responsible for calculating the value and assigning the value so the load is more on View and Controller where in MVVM View and Controller are only responsible for assigning the value not calculating the value.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, SafeZone Safety Systems
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork