MVC Vs MVVM In Swift

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

}

What Is MVC?

Everybody knows MVC stands for Model-View-Controller. Let’s see in detail.

Model

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.

View

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.

Controller

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

What Is MVVM?

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:

Model

The model is the same as what we discussed in MVC. So I am not going to discuss it here again.

View

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.

ViewModel

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.

Where Is The Controller?

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.

Source Code

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

coma

Conclusion

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.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?