SlideShare a Scribd company logo
TASK 2: Educational Article – Model View Controller (MVC)
If you are new to the world of iOS development, chances are that in a development team, you will be
required to follow Apple’s standard Model-View-Controller (​MVC​) architecture. While the adoption
of MVC may seem confusing and aimless at first, its advantages will emerge slowly throughout the
development process and save precious time for any project or application. This article aims to
educate novice iOS developers about why it is highly recommended, how to incorporate it in an
application, and how to benefit from the advantages that MVC offers.
First, let us formally understand what MVC is:
Model-View-Controller (MVC for short) is an architectural software design pattern for organizing
information, interfaces, and interactions in various applications. It broadly breaks down any
software application into three interdependent components ​– the model, the view, and the
controller ​– with the intention of keeping internal representations of information and business logic
separate from how the information is displayed to and received from the user.
But what exactly does this mean? In order to make things a less complicated, it is important to
elucidate that MVC can be understood as an ideal to follow while developing; it is an archetype of
how to organize classes and objects in Swift according to the roles they play in the final rendition of
the application.
The Components of MVC
MVC, as its name suggests, is made up of three layers: the model, the view, and the controller. Every
class or Swift file part of an application falls into the category of one of these components based on
which area of the application it supports.
1. Model
The model is for the business logic of the application, and is where all the data for the application is
contained. Database storage and retrieval, networking code, API calls, and other backend related
particulars are all part of this component.
As a rule, the model should never be concerned with how the data it contains is being displayed to
the user, or even with how the data is being modified by the user.
2. View
The view is what the user of an application physically sees and interacts with on their device. The
different interface screens, modes of display, text fields, buttons, labels, lists, and other UI elements
are all part of the view component of MVC.
View objects are devoid of domain related data and are therefore typically reusable, for example the
UIKit components part of Xcode’s library that can be used in any application. Since the view is
unrelated to the specific data it is meant to display, it may also be modified from time to time unlike
the business logic of the application.
3. Controller
The controller is the component that mediates between the model and the view. Since the model
and view are more or less disconnected from one another, controller objects do not control how
exactly the data will be presented to the user but rather use protocols and delegates to facilitate
communication in an abstract manner.
Typically, usage related questions such as what happens when the user refreshes, how does the
application run in the background, when certain changes are forgotten before the application is quit,
etc. are concerned with the controller, as it provides the logical flow of operations when the
application is being used.
The controller can be thought of as the powerhouse of the application or the glue that works in
tandem with the model and the view.
EXERCISE: Categorize This!
Now that you have a fair idea of what goes where, try to group the following items into the
respective MVC components they fall under. To make it more challenging, try not to refer back to the
description of components above.
1. A table displaying results from a Yelp search.
2. A function used to store the profile picture of a user for a social networking application.
3. A label used to present the name of a Pokémon in battle.
4. A UITableViewDataSource method that specifies the number of rows in a table.
5. The addition of the cost of items in a UITableView to calculate an individual’s total expenditure.
6. A function that parses a JSON file obtained from a weather forecasting API call.
7. An enum that determines whether a user has been logged in or logged out.
8. A stack of card objects that you can swipe left or right on.
The answers are given at the end of this page.1
Cycle of Communication between MVC Components
The following cycle is typically the sequence that is followed when an application is used, and the
MVC components interact with each other accordingly:
● A user’s action in the view communicates to the controller that something has changed.
● The controller updates the model based on the information relayed by the user’s action in the
view.
● When the model is modified based on some network call or retrieval of data for example, it
communicates to the controller that it has been updated with some new information.
● The corresponding view objects are changed based on the new data communicated to the
controller.
1
(1) View, (2) Model, (3) View, (4) Controller, (5) Controller, (6) Model, (7) Controller, (8) View
The series of events can be summarized by the diagram below:
Examples: Interaction between Model, View, and Controller
View objects may communicate with the controller through targets or delegates. Suppose the user
interacts with a button on a UI screen by tapping it. Then, the appropriate controller is designated
as the ​target for the user’s action of touching the button, and what happens next in the application
is left to the controller.
The responsibility of carrying out the result of a user action can also ​delegated to the controller. For
example, suppose there is an image picker object that is invoked when a user tries to add a photo to
an album on an iOS application. Then, the decision on what must happen once the user has selected
the image from their camera roll (or taken a photo) could be left to the controller by setting it as
the delegate of the image picker object.
The controller listens to any changes in the model and updates the view accordingly. In the above
examples, the tapping of a button could maybe sign the user in into their account on the app, and
the listening controller(s) would be notified to take the user to their home screen. Similarly, the
image picker delegate would be informed that the particular image has been stored in the database,
and it would then facilitate the action of having the image appended to the user’s album.
Advantages of Adopting MVC
Most applications today involve the use of the MVC architecture because of the benefits described
below. Knowing about the various advantages that MVC offers for an application will help you
identify these points during the process of development and use them to your advantage.
● MVC objects tend to be ​reusable​. If their interfaces are well defined in an application, they can
more easily be implemented in other similarly structured applications.
● The adoption of MVC makes code more ​readable ​and manageable ​– this is key for large
development teams where there is heavy collaboration and developers work on different
components of the application.
● Improves ​organization as the logic layer of the application is separated from what information is
displayed or presented to the user.
● Liberty in the development process: developers working on user interface and display can focus
on the physical presentation of the application without being affected by the logic and
organization of data; developers of the model can focus more on the back-end, business logic of
the application, storage and retrieval of data, etc. without being concerned about how the
information is going to be perceived by the user. This ​minimizes the interdependency of
developers.
● There can be ​multiple objects of a single MVC component, making it easier for developers to
engage with the information and its presentation (eg: multiple view objects that display the
same data in different ways).
● The MVC pattern ​allows frequent UI changes​; while the business model and logic of an
application undergoes little change over time, more radical changes pertaining to size, colour,
font, layout, structure, and other UI elements can be easily implemented as it is separate from
the model.
● The adoption of MVC isolates different components of the application and makes it easier to
identify problems and bugs while thoroughly testing functionality. The issues therefore also
become easier to fix.
Key Takeaways
It is usually tempting to write large chunks of code in the UIViewController class as it seems to be
easier on the surface level. However, in the long run, it can cause a lot of problems as the
functionality present in the view controller grows larger and larger. This is one of the primary
reasons why it is advisable to incorporate the MVC pattern as a rule of thumb when developing any
application.
At first, it can be challenging to understand every object in terms of what exact role it plays in the
application, and to make confident decisions on which class or MVC component a block of code you
wrote should fall under. The key is to always remember the main roles of the components:
● Model – Business logic/data
● View – User interface
● Controller – Glue or mediator
In general, when you find that a view controller is starting to have an unnecessarily large amount of
code in it, it is probably wiser to write a new class and reorganize the functionality of the view
controller. Sometimes, it may also be helpful to think, ​Is this something I can reuse in other areas of
my application? ​Does this pertain solely to the data of my application or to a UI element? If you
answer yes to any of these, chances are that the code can be incorporated into the model or view
component rather than in the controller.
While there is breeding ground for debate on the virtual platforms out there in the world, the MVC
architectural pattern is widely recommended by most. It makes development and testing easier,
faster, and more collaboration-friendly, and while it may be hard to believe and a pain to employ
now, it will save you (and your team) precious amounts of effort over the course of time.

More Related Content

What's hot (9)

PPTX
Asymetric Modernization
Peter Presnell
 
PDF
Super applied in a sitecore migration project
dodoshelu
 
PDF
The Significant role of event driven apps in software development
Shelly Megan
 
DOCX
SOFTWARE REQUIREMENT SPECIFICATION FOR PROJECT
Ajeet Singh
 
PDF
Managing requirements by using baselines
IBM Rational software
 
PDF
Microsoft SharePoint 2013 : The Ultimate Enterprise Collaboration Platform
Edureka!
 
PDF
Web application architecture guide how it works types, components, best pract...
Katy Slemon
 
PDF
Personalizing Photos Application
Mike Taylor
 
Asymetric Modernization
Peter Presnell
 
Super applied in a sitecore migration project
dodoshelu
 
The Significant role of event driven apps in software development
Shelly Megan
 
SOFTWARE REQUIREMENT SPECIFICATION FOR PROJECT
Ajeet Singh
 
Managing requirements by using baselines
IBM Rational software
 
Microsoft SharePoint 2013 : The Ultimate Enterprise Collaboration Platform
Edureka!
 
Web application architecture guide how it works types, components, best pract...
Katy Slemon
 
Personalizing Photos Application
Mike Taylor
 

Viewers also liked (19)

PPTX
Case presentation
Ibrahim Abdullah
 
PPT
Technol2
Lauren
 
PPTX
Magazine mind map
hannah_hughes
 
PPT
Say no to gender based violence
leticiaeoimairena
 
PDF
Jane Kusiak
LEAD VIRGINIA
 
PPT
найкращі поезії про кохання
Nastia Dziubak
 
PDF
Parola d’ordine: differenziare!
Hospitality Rest@rt Tropea
 
PPT
Sprogteknologi som oversætterens arbejdsplads bjarne poulsen
Bjarne Poulsen
 
PPT
LinkedIn Presentation to BUA Network South East Ireland
Denis
 
PPT
Zozaws
Wannaspeak
 
ODP
Erlazino funtzinoak
bermeokokuxkuxeroa6a16
 
PDF
Transferencia de los distritos de riego
Academia de Ingeniería de México
 
PDF
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive Conference
 
PPTX
Musicmagazinefinalpitch 131030174627-phpapp02
amorrison1
 
PPTX
The next big thing .
Annepaga Pawankumar
 
PPTX
Final ppt of financial mgmt
Payal Tharani
 
PPS
Ходосевич Т.А.
Anastasia Zybina
 
DOC
Teori bahan isolasi-Syamsir Abduh
Trisakti University
 
PPTX
Alyson Martinez
MrLawler
 
Case presentation
Ibrahim Abdullah
 
Technol2
Lauren
 
Magazine mind map
hannah_hughes
 
Say no to gender based violence
leticiaeoimairena
 
Jane Kusiak
LEAD VIRGINIA
 
найкращі поезії про кохання
Nastia Dziubak
 
Parola d’ordine: differenziare!
Hospitality Rest@rt Tropea
 
Sprogteknologi som oversætterens arbejdsplads bjarne poulsen
Bjarne Poulsen
 
LinkedIn Presentation to BUA Network South East Ireland
Denis
 
Zozaws
Wannaspeak
 
Erlazino funtzinoak
bermeokokuxkuxeroa6a16
 
Transferencia de los distritos de riego
Academia de Ingeniería de México
 
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive Conference
 
Musicmagazinefinalpitch 131030174627-phpapp02
amorrison1
 
The next big thing .
Annepaga Pawankumar
 
Final ppt of financial mgmt
Payal Tharani
 
Ходосевич Т.А.
Anastasia Zybina
 
Teori bahan isolasi-Syamsir Abduh
Trisakti University
 
Alyson Martinez
MrLawler
 
Ad

Similar to Task 2 - Educational Article – Model View Controller (MVC) (20)

PPTX
iOS Coding Best Practices
Jean-Luc David
 
PDF
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
PDF
Introduction of Xcode
Dhaval Kaneria
 
PPTX
Mvc pattern and implementation in java fair
Tech_MX
 
PPT
Mvc architecture
Surbhi Panhalkar
 
PPTX
MVC.pptx
HassanAliKhan36
 
PDF
iOS architecture patterns
allanh0526
 
PPT
MVC(Model View Controller),Web,Enterprise,Mobile
naral
 
PPT
Web engineering - MVC
Nosheen Qamar
 
PDF
How I Accidentally Discovered MVVM
Bradford Dillon
 
PPTX
Model View Controller ext4
Pankaj Avhad
 
PDF
Ui design patterns
Jorge Ortiz
 
PPTX
MWLUG 2015 - An Introduction to MVC
Ulrich Krause
 
PPTX
An Introduction To Model  View  Controller In XPages
Ulrich Krause
 
PDF
Model-View-Controller: Tips&Tricks
Ciklum Ukraine
 
PDF
MVC Seminar Presantation
Abhishek Yadav
 
PPTX
Class 02 Objective C
Violeta Salas
 
PPT
Ppt of Basic MVC Structure
Dipika Wadhvani
 
ODP
Model View Controller
Madhukar Kumar
 
iOS Coding Best Practices
Jean-Luc David
 
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
Introduction of Xcode
Dhaval Kaneria
 
Mvc pattern and implementation in java fair
Tech_MX
 
Mvc architecture
Surbhi Panhalkar
 
MVC.pptx
HassanAliKhan36
 
iOS architecture patterns
allanh0526
 
MVC(Model View Controller),Web,Enterprise,Mobile
naral
 
Web engineering - MVC
Nosheen Qamar
 
How I Accidentally Discovered MVVM
Bradford Dillon
 
Model View Controller ext4
Pankaj Avhad
 
Ui design patterns
Jorge Ortiz
 
MWLUG 2015 - An Introduction to MVC
Ulrich Krause
 
An Introduction To Model  View  Controller In XPages
Ulrich Krause
 
Model-View-Controller: Tips&Tricks
Ciklum Ukraine
 
MVC Seminar Presantation
Abhishek Yadav
 
Class 02 Objective C
Violeta Salas
 
Ppt of Basic MVC Structure
Dipika Wadhvani
 
Model View Controller
Madhukar Kumar
 
Ad

Task 2 - Educational Article – Model View Controller (MVC)

  • 1. TASK 2: Educational Article – Model View Controller (MVC) If you are new to the world of iOS development, chances are that in a development team, you will be required to follow Apple’s standard Model-View-Controller (​MVC​) architecture. While the adoption of MVC may seem confusing and aimless at first, its advantages will emerge slowly throughout the development process and save precious time for any project or application. This article aims to educate novice iOS developers about why it is highly recommended, how to incorporate it in an application, and how to benefit from the advantages that MVC offers. First, let us formally understand what MVC is: Model-View-Controller (MVC for short) is an architectural software design pattern for organizing information, interfaces, and interactions in various applications. It broadly breaks down any software application into three interdependent components ​– the model, the view, and the controller ​– with the intention of keeping internal representations of information and business logic separate from how the information is displayed to and received from the user. But what exactly does this mean? In order to make things a less complicated, it is important to elucidate that MVC can be understood as an ideal to follow while developing; it is an archetype of how to organize classes and objects in Swift according to the roles they play in the final rendition of the application. The Components of MVC MVC, as its name suggests, is made up of three layers: the model, the view, and the controller. Every class or Swift file part of an application falls into the category of one of these components based on which area of the application it supports. 1. Model The model is for the business logic of the application, and is where all the data for the application is contained. Database storage and retrieval, networking code, API calls, and other backend related particulars are all part of this component. As a rule, the model should never be concerned with how the data it contains is being displayed to the user, or even with how the data is being modified by the user. 2. View The view is what the user of an application physically sees and interacts with on their device. The different interface screens, modes of display, text fields, buttons, labels, lists, and other UI elements are all part of the view component of MVC. View objects are devoid of domain related data and are therefore typically reusable, for example the UIKit components part of Xcode’s library that can be used in any application. Since the view is unrelated to the specific data it is meant to display, it may also be modified from time to time unlike the business logic of the application.
  • 2. 3. Controller The controller is the component that mediates between the model and the view. Since the model and view are more or less disconnected from one another, controller objects do not control how exactly the data will be presented to the user but rather use protocols and delegates to facilitate communication in an abstract manner. Typically, usage related questions such as what happens when the user refreshes, how does the application run in the background, when certain changes are forgotten before the application is quit, etc. are concerned with the controller, as it provides the logical flow of operations when the application is being used. The controller can be thought of as the powerhouse of the application or the glue that works in tandem with the model and the view. EXERCISE: Categorize This! Now that you have a fair idea of what goes where, try to group the following items into the respective MVC components they fall under. To make it more challenging, try not to refer back to the description of components above. 1. A table displaying results from a Yelp search. 2. A function used to store the profile picture of a user for a social networking application. 3. A label used to present the name of a Pokémon in battle. 4. A UITableViewDataSource method that specifies the number of rows in a table. 5. The addition of the cost of items in a UITableView to calculate an individual’s total expenditure. 6. A function that parses a JSON file obtained from a weather forecasting API call. 7. An enum that determines whether a user has been logged in or logged out. 8. A stack of card objects that you can swipe left or right on. The answers are given at the end of this page.1 Cycle of Communication between MVC Components The following cycle is typically the sequence that is followed when an application is used, and the MVC components interact with each other accordingly: ● A user’s action in the view communicates to the controller that something has changed. ● The controller updates the model based on the information relayed by the user’s action in the view. ● When the model is modified based on some network call or retrieval of data for example, it communicates to the controller that it has been updated with some new information. ● The corresponding view objects are changed based on the new data communicated to the controller. 1 (1) View, (2) Model, (3) View, (4) Controller, (5) Controller, (6) Model, (7) Controller, (8) View
  • 3. The series of events can be summarized by the diagram below: Examples: Interaction between Model, View, and Controller View objects may communicate with the controller through targets or delegates. Suppose the user interacts with a button on a UI screen by tapping it. Then, the appropriate controller is designated as the ​target for the user’s action of touching the button, and what happens next in the application is left to the controller. The responsibility of carrying out the result of a user action can also ​delegated to the controller. For example, suppose there is an image picker object that is invoked when a user tries to add a photo to an album on an iOS application. Then, the decision on what must happen once the user has selected the image from their camera roll (or taken a photo) could be left to the controller by setting it as the delegate of the image picker object. The controller listens to any changes in the model and updates the view accordingly. In the above examples, the tapping of a button could maybe sign the user in into their account on the app, and the listening controller(s) would be notified to take the user to their home screen. Similarly, the image picker delegate would be informed that the particular image has been stored in the database, and it would then facilitate the action of having the image appended to the user’s album. Advantages of Adopting MVC Most applications today involve the use of the MVC architecture because of the benefits described below. Knowing about the various advantages that MVC offers for an application will help you identify these points during the process of development and use them to your advantage. ● MVC objects tend to be ​reusable​. If their interfaces are well defined in an application, they can more easily be implemented in other similarly structured applications. ● The adoption of MVC makes code more ​readable ​and manageable ​– this is key for large development teams where there is heavy collaboration and developers work on different components of the application.
  • 4. ● Improves ​organization as the logic layer of the application is separated from what information is displayed or presented to the user. ● Liberty in the development process: developers working on user interface and display can focus on the physical presentation of the application without being affected by the logic and organization of data; developers of the model can focus more on the back-end, business logic of the application, storage and retrieval of data, etc. without being concerned about how the information is going to be perceived by the user. This ​minimizes the interdependency of developers. ● There can be ​multiple objects of a single MVC component, making it easier for developers to engage with the information and its presentation (eg: multiple view objects that display the same data in different ways). ● The MVC pattern ​allows frequent UI changes​; while the business model and logic of an application undergoes little change over time, more radical changes pertaining to size, colour, font, layout, structure, and other UI elements can be easily implemented as it is separate from the model. ● The adoption of MVC isolates different components of the application and makes it easier to identify problems and bugs while thoroughly testing functionality. The issues therefore also become easier to fix. Key Takeaways It is usually tempting to write large chunks of code in the UIViewController class as it seems to be easier on the surface level. However, in the long run, it can cause a lot of problems as the functionality present in the view controller grows larger and larger. This is one of the primary reasons why it is advisable to incorporate the MVC pattern as a rule of thumb when developing any application. At first, it can be challenging to understand every object in terms of what exact role it plays in the application, and to make confident decisions on which class or MVC component a block of code you wrote should fall under. The key is to always remember the main roles of the components: ● Model – Business logic/data ● View – User interface ● Controller – Glue or mediator In general, when you find that a view controller is starting to have an unnecessarily large amount of code in it, it is probably wiser to write a new class and reorganize the functionality of the view controller. Sometimes, it may also be helpful to think, ​Is this something I can reuse in other areas of my application? ​Does this pertain solely to the data of my application or to a UI element? If you answer yes to any of these, chances are that the code can be incorporated into the model or view component rather than in the controller. While there is breeding ground for debate on the virtual platforms out there in the world, the MVC architectural pattern is widely recommended by most. It makes development and testing easier, faster, and more collaboration-friendly, and while it may be hard to believe and a pain to employ now, it will save you (and your team) precious amounts of effort over the course of time.