MVC, MVVM, And MVP Design Patterns in Swift _ by Knyaz Harutyunyan _ Medium
MVC, MVVM, And MVP Design Patterns in Swift _ by Knyaz Harutyunyan _ Medium
Let’s dive into the MVC architecture and gain a deeper understanding of how
it works 🔥
# M — Model
The “Model” is responsible for business logic, network calls, parse data, and
so on. The “Model” does not need to know about the UI if you in the “Model”
use UI attributes it means your architecture goes wrong.
# V — View
The ‘View’ is responsible for representing UI elements such as UILabel,
UIView, UITextView, and so on. It should not have knowledge of the ‘Model.’
Including business logic and network calls within the ‘View’ indicates a
problem in your architecture.
# C — Controller
The “Controller” acts as an intermediary between the “Model” and the
“View”. It handles user input, communicates with the “Model” to fetch or
update data, and updates the “View” accordingly. You can see the above
image 🤷♂️
# The real example of MVC architecture in UIKit 🥶
You can check out my GitHub repository at the following link: 👉
TodoListApp. The ‘TodoListApp’ is built using the MVC architecture.
The MVVM architecture is quite similar to the MVC design pattern. While it
shares many similarities with MVC, MVVM is widely used in SwiftUI, and it’s
especially beneficial when working with the Combine framework.
# M — Model
The “Model” in MVVM is similar to the “Model” in MVC. It represents the
data and business logic of the application. It defines how data is structured
and manipulated. In Swift, the “Model” can be implemented using classes,
structs, or enums.
# V — View
The “View” in MVVM is also similar to the “View” in MVC. It represents the
only UI elements and the “View” should only display data, and handle user
interactions, without containing application logic(business logic).
# VM — View-Model
The “ViewModel” acts as a mediator between the “Model” and the “View”.
The “ViewModel” is responsible for preparing and formatting data from the
“Model” for presentation in the “View”. “ViewModels” also handles user
input and interactions. They contain the business logic needed for specific
views.
struct Task {
var title: String
var completed: Bool
}
# ViewModel
class TaskListViewModel {
private var tasks: [Task] = [
Task(title: "Buy groceries", completed: false),
Task(title: "Read a book", completed: true),
Task(title: "Exercise", completed: false)
]
# View
import UIKit
The MVP architecture is also similar to MVC architecture the same “Model”
layer, “View” layer, and additional “Presenter”. However, the MVP design
pattern has one difference from the MVC design pattern. Now the
ViewController(Controller layer) is considered as a view. which means it
will include only the view-related code, nothing more and all logic will be
implemented in the presenter.
View: The “View” includes both view and view controllers, taking care of
setting up the user interface and handling user interactions and all
events.
Model: The “Model” role will be exactly the same in MVC architecture.
# View
# Presenter
init(task: Task) {
self.task = task
}
func taskCompleted() {
// Perform logic to mark the task as completed
}
}
Task: The task represents the “Model”, which defines the structure of a
task.