Mmvmi: A Validation Model For MVC and MVVM Design Patterns in Ios Applications
Mmvmi: A Validation Model For MVC and MVVM Design Patterns in Ios Applications
______________________________________________________________________________________
Abstract—Design patterns have gained popularity as they not work as desired as it will lead to a famous problem
provide strategies for solving specific problems. This paper in iOS community: the Massive View Controller problem.
explores two common design patterns in iOS development field: When Controller objects exceed 150 lines, this is an indicator
Model View Controller (MVC) and Model View ViewModel
(MVVM) design patterns. The paper investigates the problems of Massive View Controller problem. Controller objects will
with MVC design pattern. Then, it introduces a validation be massive when they handle many responsibilities more than
model that detects MVC problems, and helps programmers their own. The Model View ViewModel (MVVM) design
make the decision to switch between MVC and MVVM design pattern rescuers gracefully by strictly distributing the roles
pattern. The proposed validation model consists of two phases: and responsibilities among objects [6]. By using MVVM
phase one is to detect MVC problems, and phase two is
to validate the relation between MVVM objects. The model design pattern Controller objects will just set the values
was then applied to a Cloudy app as a case study. As a for UI components while all data logic preparation for UI
result, the model was able to detect MVC problems. The will be located in ViewModel objects. As a rule of thumb,
relation between MVVM objects was also validated. Further, both of MVC and MVVM have their own advantages and
this research provided some recommended solutions to satisfy disadvantages, choosing one of them as a design pattern is
the relations between MVVM objects in the project.
highly dependents on the project architecture. For instance,
Index Terms—design pattern, mvc, mvvm, validation, model. JavaScript has different frameworks each of them used either
MVC or MVVM according to the framework needs. Namely
AngularJS, Angular 2, and Vue.js are based on MVC, while
I. I NTRODUCTION Ember.js is built based on MVVM [7]. In essence, the
objectives of this study are:
VER the years, softwares have been grown and
O changed to accommodate users’ demands. Users need
to interact with softwares through interfaces, where the
1) To understand the impact, role and importance of
Design Patterns in iOS development.
2) To help developers to make a decision when to use
emerge of interfaces has catalyzed a new demand of design MVC or MVVM.
patterns to help keep interfaces user friendly and to improve 3) To provide a method to check and validate the relations
them [1]. Since then, several design patterns appeared then between MVVM objects.
evolved and matured [2]. Design patterns reuse the same set To address these objectives, we proposed a model that will
of ideas to construct a solution to solve certain problems help developers to indicate the time to switch from MVC
occur commonly [3]. Model View Controller (MVC) design design pattern to MVVM design pattern. The proposed model
pattern is used frequently to architect interactive software consists of two phases. Phase one will detect if the project
systems [4]. MVC design pattern offers a way to struct has a Massive View Controller problem. The second phase,
application’s components into different separate roles [3]. will check the relations between project’s objects if they obey
In fact, recent technologies encourage separating the roles to MVVM correctly or not. Hence, there are three significant
of application early in design phase [4]. Thus, roles sepa- contributions in this paper. First, MVC problems is presented.
rating improves the implementation of the application as it Then, this paper develops a validation model that accomplish
composes a robust and flexible application. MVC separates these objectives: detecting massive controller objects, and
roles as follow: View objects in MVC displays data to users, validate the relations between MVVM objects.
while user interaction is processing by Controller objects
The remainder of this paper is organized as follows:
[4]. Application logic and data are the responsibility of
section 2 provides a background knowledge of Model View
Model objects [4]. The Model View Controller (MVC) de-
Controller (MVC) and Model View ViewModel (MVVM)
sign pattern is recommended by Apple since its frameworks
design patterns. Section 3 presents the related work. Sec-
built based on it [5]. Consequently, any iOS developer will
tion 4 introduces MMVMi, our model to validate MVC or
start programming an iOS application based on Model View
MVVM in iOS applications, and explains its two phases. Fol-
Controller (MVC) design pattern. For some projects MVC
lowed by applying MMVMi to two cases and demonstrating
will work perfectly: as desired leading to reusable objects,
the results. After that, MMVMi evaluation by comparing it
easily extensible objects and much more numerous benefits.
with other tools in section 6. The conclusion is described in
However, for some other projects, MVC design pattern will
section 7. The paper concludes with a future work in section
Manuscript received October 3, 2017; revised May 9, 2018.
8.
M. Aljamea is a graduate student in the Department of Computer
Engineering at College of Engineering and Petroleum - Kuwait University,
Kuwait e-mail: (mrm259@gmail.com). II. BACKGROUND
M. Alkandari is an Assistant Professor in the Department of Computer
Engineering at College of Engineering and Petroleum - Kuwait University, Design patterns have been part of application development
Kuwait e-mail: (m.kandari@ku.edu.kw). process as they help and guide developers to develop efficient
in iOS community that determine the massive controller as of UIView or any of its subclasses such as: UIButton,
the following: when controller has more than 300 lines, UITableViewCell, UILabel, UIImageView, UISwitch and the
it is a massive controller. In contrast, any controller that rest of the subclasses that UIKit provides. All these view
has less than 150 lines is considered as a thin controller. classes are provided by UIKit framework for iOS, while
Another key contribution to mention, MCD aims to help OS X view classes are provided by AppKit framework [12].
programmers more and to let them get the most benefits of Furthermore, there are dozens of View objects in Interface
the proposed model, so Massive Controller Detection model Builder Library [12]. On the other hand, there is no direct or
warns them when it detects a controller which has 150 lines simple way to find Model objects, and ViewModel objects
or more but less than or equal 300 lines. This warning is because they are not subclasses of known classes. In addition,
important because keeping controller objects less than 150 programmers create Model objects, and ViewModel objects
lines not only helps in testing but also helps in maintaining in variety ways. As a result, when users run ORV script,
the project, which in terms, helps improving the performance it will ask users to enter the name of Model objects, and
of the project overall. It is obvious that programmers write ViewModel objects. Then, ORV will generate a dot file
comments to help them in several ways such as documen- that has the attributed graph text. As shown in Algorithm
tations, or posting comments for other programmers. Also, 2, in line 1 to generate the dot file, the script will col-
programmers insert new lines to make the file readable and lect all MVVM objects: Controller objects, View objects,
to follow conventions in coding style. With these two reasons ViewModel objects, and Model objects. Then, in line 2 the
in mind, Massive Controller Detection model counts the lines script will start writing each object in its cluster: Controller
in an accurate way by ignoring all comments and all empty objects in controller cluster, View objects in view cluster,
lines. ViewModel objects in view model cluster, and Model objects
in model cluster. After that, in line 3 ORV will open each
B. Phase two: Object Relations Validation (ORV) file (object) of MVVM and read it to check if the object
has a relation with other objects. If the relation exist, ORV
After phase one, object relations validation (ORV) phase
will connect the objects, otherwise ORV will not generate
comes, ORV used dot [27] language to draw a directed
a connection. Also, ORV will check the existing relations
graph that represents the relations between objects in iOS
are they obey to MVVM rules or not. Then, ORV will draw
project. These objects are MVVM objects: Model object,
all illegal relations with a dotted arrow as written in line
View object, and ViewModel object. The generated graph
6 in Algorithm 2. Moreover, ORV will search carefully to
from ORV shows how MVVM objects are related to each
detect the exact line number that is the reason of the wrong
other. For wrong relations cases, ORV will draw these wrong
relation. After that, ORV will display this line number in a
edges with a dotted arrow. It will also detect the exact line
warning message. To accomplish this checking successfully,
number which cases the wrong relation and display it to the
ORV used python regular expression (RE) pattern to find a
user. The methodology of object relations validation (ORV)
match. The used RE pattern is accurate since it checks the
phase will be explained in the next subsection.
file line by line: searching for other objects if they exist in
this file. Moreover, sometimes programmers name methods
Algorithm 2 MVVM objects relations validation in iOS
or variables with the same name of other files or part of
project
it. For example, suppose we have a Model object its name:
INPUT: iOS project directory F . Students, and suppose that in ViewModel object there is a
OUTPUT: A directed graph with all relations between method named: findAllStudents. ORV used word boundaries
MVVM objects. in the used RE pattern, therefore it will be intelligent enough
to ignore findAllStudents and avoid partial matches. The
1: for all MVVMObjects fi in F do result of running ORV script is a dot file that has all the
2: write fi in its cluster relations between MVVM objects written as attributed graph
3: if fi has a relation with any other MVVM Objects text. Finally, the user will run the dot file to generate the
then directed graph that depicts the relations between MVVM
4: draw the relation objects in the project. If there is any wrong relation, ORV
5: if fi is a wrong relation then will depict a dotted arrow. For instance, according to Model
6: draw it with a dotted arrow View ViewModel design pattern, it is not allowed for Model
7: detect the line number that causes the wrong objects to communicate with View objects. Consequently, if
relation ORV finds Model object that is communicated with View ob-
8: display a WARNING message jects, ORV will draw this relation arrow with a dotted arrow
9: end if to indicate that this relation is a wrong relation. Thereby,
10: end if object relations validation phase will help programmers or
11: end for developers to construct a robust application architecture by
writing a valid MVVM design pattern with correct relations
Object Relations Validation (ORV) Methodology: ORV between all objects.
follows the same strategy that is used in MCD to find Figure 5 shows the flow chart of MVC & MVVM Validation
all Controller objects. Additionally ORV will find all View Model for iOS (MMVMi). Any iOS project can be written
objects. In order to find View objects, ORV modifies MCD by following Model View Controller design pattern, Model
pattern regular expression instead of finding all UIViewCon- View ViewModel design pattern, or mix of them, or even
troller. It will find all View objects (files) which are subclass any other available design patterns. As depicted in Figure
Start
iOS
project
directory
MCD
no
Fig. 6. Cloudy App: (a) Main view, and (b) Settings view
ORV
Fig. 10. Object Relations Validation with relations between objects in the same cluster
Fig. 12. Warning message for wrong relations between MVVM objects
VI. E VALUATION AND RESULTS Fig. 13. Invalid relations between MVVM objects
Visualizer tool. The first subsection, evaluates Massive Con- Fig. 15. Detecting massive controllers by using Krzysztof Zablocki script
on Cloudy MVVM version
troller Detection phase. While Object Relations Validation
phase is evaluated in the next two subsections. Subsection
two, compares Object Relations Validation phase with De-
pcheck by running both of them on Cloudy app MVVM
version. The next subsection, applies Objc Dependency Visu-
alizer on Cloudy app MVVM version. All results and graphs
output of the aforementioned tools will be discussed in detail
in this section.
Cloudy App MVVM version. Roughly, this graph helps view dismissed all these ViewModel nodes relations because these
the relations between objects in the project. However, the relations are not with other ViewModel nodes. Indeed, de-
graph is huge which makes it unclear and difficult to find a velopers need to check how ViewModel nodes are linked
specific relation. On the contrary, Figure 9 depicts MMVMi with Model nodes and Controller nodes. This relations check
dependency graph for the same project: Cloudy App MVVM is clearly illustrates in MMVMi graph as shown in Figure
version. Definitely MMVMi graph is more helpful than 9. Moreover, filter option can be considered as a drawback
Depcheck graph for several reasons. Such as MMVMi graph because in order to use it, users must have a good knowledge
omits unnecessary relations, whereas Depcheck graph draws in regular expression to write a regular expression that fulfills
all relations between objects even the relation between the their need.
object and itself as shown in Figure 17: RootViewController
has an arrow from its node to itself. Moreover, MMVMi VII. C ONCLUSION
graph assists developers to observe the relations between
MVVM objects easily. However, using Depcheck graph Design patterns have a huge impact that affects several
might help but in a complex way as it has numerous arrows. aspects of software engineering including development, and
architecture. In this work, we focused on iOS application
development with two design patterns: Model View Con-
C. Comparison with Objc Dependency Visualizer
troller (MVC) design pattern, and Model View ViewModel
Objc dependency visualizer tool illustrates the relations (MVVM) design pattern. MVC suffers from many limitations
between classes in iOS project in an interactive way. In as discussed earlier. Consequently, developers tend to use
this subsection, objc dependency visualizer graph will be Model View ViewModel to overcome MVC limitations. The
compared with MMVMi graph. Figure 18 displayed objc overall objective of our work is to help developers to stick
dependency visualizer graph for Cloudy MVVM version. In to the rules of MVC design pattern, and help them to check
the top right corner, there is a Live editor that gives users and detect if they have Massive View Controller problem. In
the ability to control circle size, charge multiplier, and link this case, they have to switch to MVVM design pattern. We
strength. Users can control all these options in a simple way aim to advice developers to switch to MVVM design pattern
by moving and adjusting the sliders according to their needs. as it separates concerns in a better way and it distributes the
Also, there is ”show names” option where when it checked roles more productively. To achieve these objectives, we have
all classes name will be appeared on their corresponding developed a validation model: MVC & MVVM Validation
nodes. Last but not least, filter option where users can type Model for iOS (MMVMi). The presented validation model
a regular expression to filter nodes. By comparing Figure 9 addressed our goals and aims as it helps developers to
with Figure 18, it is clear that Figure 18 shows how each validate their projects with MVC rules. The proposed model
class is linked to other classes which made it crowded and validates the relations between project objects and check if
messy. Therefore, in Figure 19 we applied filter to show just they satisfy the MVVM objects relations rules. We assert that
Controller objects. After applying Controller objects filter, such a validation model will aid developers and programmers
the arrows relations will be illustrated between only nodes to successfully apply MVC or MVVM on their iOS projects
that have controller within their name. It will be better if the and get the main benefits from applying design pattern
Live editor has an option to omit unrelated nodes, and other correctly. We hope this work will help iOS community and
option to show the relations between the filtered nodes and guide them to build robust iOS applications that are well
other nodes. structured with a valid design pattern.
Figure 20 depicts ViewModel filter, the filter is re-
ally useless as it just shows the relation between
WeekViewViewModel node and WeatherDayViewView- VIII. F UTURE WORK
Model node. However, there are many ViewModel Currently, Object Relations Validation phase in MMVMi
nodes including DayViewViewModel, SettingsViewTemper- used dot language to produce the directed graph. We see that
atureViewModel, SettingsViewTimeViewModel, and Set- this is an aspect that needs to be improved. The output of
tingsViewUnitsViewModel. Objc dependency visualizer tool ORV phase is a simple directed graph that does not allow
Fig. 18. Objc dependency visualizer output for Cloudy MVVM version
the user to interact with it. User interactions can come in [6] B. Orlov, “ios architecture patterns,” https://medium.com/
different forms such as giving the user the ability to move ios-os-x-development/ios-architecture-patterns-ecba4c38de52,
(Accessed on 04/03/2018).
a cluster or nodes to anyplace. All these interactions can [7] T. Lee and T. Brunner, “A prototype to increase social networking
be done with d3js library and they will be visualized in a between staff: A web application for companies,” 2017.
powerful interactive way. In fact, in iOS community there [8] A. Freeman, Pro design patterns in swift. Apress, 2015.
[9] J. Patel, S. Okamoto, S. M. Dascalu, and F. C. Harris Jr, “Web-
are many projects that use both design patterns: Model View enabled toolkit for data interoperability support,” in Proceedings of
Controller, and Model View ViewModel. So, we plan to the 21th International Conference on Software Engineering and Data
improve the proposed model in which, the model should have Engineering (SEDE-2012), Los Angeles, CA, 2012, pp. 161–166.
the ability to specify the project type, and it should be able [10] A. Syromiatnikov and D. Weyns, “A journey through the land of
model-view-design patterns,” in Software Architecture (WICSA), 2014
to deal with projects that use both of MVC, and MVVM. IEEE/IFIP Conference on. IEEE, 2014, pp. 21–30.
Hence, the aforementioned improvements will enhance the [11] M. Neuburg, IOS 8 Programming Fundamentals with Swift: Swift,
MMVMi and will make it more powerful. Xcode, and Cocoa Basics. ” O’Reilly Media, Inc.”, 2015.
[12] “Model-view-controller,” https://developer.apple.com/library/content/
documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html,
(Accessed on 04/03/2018).
R EFERENCES [13] A. Allan, Learning iOS Programming: From Xcode to App Store. ”
O’Reilly Media, Inc.”, 2013.
[1] E. Sorensen and M. Mikailesc, “Model-view-viewmodel (mvvm) de- [14] R. B’far, Mobile computing principles: designing and developing
sign pattern using windows presentation foundation (wpf) technology,” mobile applications with UML and XML. Cambridge University Press,
MegaByte Journal, vol. 9, no. 4, pp. 1–19, 2010. 2004.
[2] R. Vice and M. S. Siddiqi, MVVM Survival Guide for Enterprise [15] K. Topley, F. Olsson, J. Nutting, D. Mark, and J. LaMarche, Beginning
Architectures in Silverlight and WPF. Packt Publishing Ltd, 2012. IPhone Development with Swift: Exploring the IOS SDK. Apress,
[3] D. Patterns and C. Pattern, “Model-view-controller,” 2014.
Microsoft Patterns & Practices, http://msdn. microsoft. [16] H. Sun, J. Zhang, G. Sun, and Y. Li, “Agricultural traceable and
com/practices/type/Patterns/Enterprise/DesMVC, 2003. marketing system based on ios-platform and wireless sensor network,”
[4] A. Leff and J. T. Rayfield, “Web-application development using the Journal of Computer and Communications, vol. 5, no. 06, p. 45, 2017.
model/view/controller design pattern,” in Enterprise Distributed Object [17] L. Nguyen and K. Nguyen, “Application of protocol-oriented mvvm
Computing Conference, 2001. EDOC’01. Proceedings. Fifth IEEE architecture in ios development,” 2017.
International. IEEE, 2001, pp. 118–127. [18] K. Peterson, “The github open source development process,” url:
[5] D. A. Lo, Sensor Plot Kit: An iOS Framework for Real-time plotting http://kevinp.me/github-process-research/github-processresearch.pdf
of Wireless Sensors. University of California, Irvine, 2015. (visited on 05/11/2017), 2013.
[19] L. Dabbish, C. Stuart, J. Tsay, and J. Herbsleb, “Social coding in Mariam Aljamea earned bachelor degree in computer engineering from
github: transparency and collaboration in an open software repository,” Kuwait University in 2012. Currently she is a master student in computer
in Proceedings of the ACM 2012 conference on Computer Supported engineering at Kuwait University and she is working on distribution systems
Cooperative Work. ACM, 2012, pp. 1277–1286. using Apache Spark for her thesis. Also, she is an iOS Developer in Ministry
[20] “Github - k0nserv/luft: The xcode plugin that helps you write of Education. She is a researcher in apache spark and bioinformatics.
lighter view controllers,” https://github.com/k0nserv/luft, (Accessed on
04/03/2018).
[21] “Github-paultaykalo/objc-dependency-visualizer: Objective-c and
swift dependency visualizer. it’s tool that helps to visualize current
state of your project. it’s really easy to see how tight your classes are
coupled.” https://github.com/PaulTaykalo/objc-dependency-visualizer,
(Accessed on 04/03/2018).
[22] “Github-wojteklu/depcheck: Dependency analyzer tool for swift
projects,” https://github.com/wojteklu/depcheck, (Accessed on
04/03/2018).
[23] “Controller object,” https://developer.apple.com/library/content/
documentation/General/Conceptual/DevPedia-CocoaCore/
ControllerObject.html#//apple ref/doc/uid/TP4000895-CH11-SW1, Mohammad Alkandari is an Assistant Professor of computer engineering
(Accessed on 04/04/2018). at Kuwait University, Kuwait, where he has been on the faculty since 2012.
[24] “Uiviewcontroller-uikit — apple developer documentation,” He received his Ph.D. degree in computer science at College of Engineering
https://developer.apple.com/documentation/uikit/uiviewcontroller, from Virginia Polytechnic Institute and State University (Virginia Tech).
(Accessed on 04/03/2018). He was the director of the Office of Engineering Education Technology at
[25] S. Brin, “Extracting patterns and relations from the world wide web,” Kuwait University, College of Engineering and Petroleum for 3 years. He
in International Workshop on The World Wide Web and Databases. is currently the coordinator of Software and Systems Engineering Research
Springer, 1998, pp. 172–183. Group at Computer Engineering Department. He is a researcher in software
[26] H. Hosoya and B. Pierce, “Regular expression pattern matching for engineering, requirements engineering, software project management, soft-
xml,” in ACM SIGPLAN Notices, vol. 36, no. 3. ACM, 2001, pp. ware quality assurance, privacy and data protection, and human-computer
67–80. interaction.
[27] E. Koutsofios, S. North et al., “Drawing graphs with dot,” Technical
Report 910904-59113-08TM, AT&T Bell Laboratories, Murray Hill,
NJ, Tech. Rep., 1991.
[28] “bartjacobs/cloudy at mvc,” https://github.com/bartjacobs/Cloudy/tree/
mvc, (Accessed on 04/03/2018).
[29] “bartjacobs/cloudy: Mastering model-view-viewmodel with swift,”
https://github.com/bartjacobs/Cloudy, (Accessed on 04/03/2018).