1 00:00:00,670 --> 00:00:02,040 The way we've modeled our app so 2 00:00:02,040 --> 00:00:06,070 far follows a very important design pattern in iOS development. 3 00:00:06,070 --> 00:00:10,030 When writing code, there are always common problems faced by all developers. 4 00:00:10,030 --> 00:00:13,580 For example, one of those problems is how to architect our apps so 5 00:00:13,580 --> 00:00:16,770 that our components are largely reusable. 6 00:00:16,770 --> 00:00:20,960 Solutions to these common problems are known as design patterns, and while you 7 00:00:20,960 --> 00:00:25,550 weren't explicitly aware of it, we've been implementing a design pattern all along. 8 00:00:25,550 --> 00:00:29,970 A common way of structuring code in your apps is using the Model, View, 9 00:00:29,970 --> 00:00:32,200 Controller design pattern or MVC. 10 00:00:33,200 --> 00:00:35,650 In the MVC pattern, objects or 11 00:00:35,650 --> 00:00:41,670 code in your app assume one of three roles, a Model, View or a Controller. 12 00:00:41,670 --> 00:00:43,380 Let's start with the model. 13 00:00:43,380 --> 00:00:47,210 All the data in our app as well as the logic around that data, 14 00:00:47,210 --> 00:00:50,030 whether you are manipulating the data in some way or 15 00:00:50,030 --> 00:00:54,640 using that data to compute new values, all that code goes in a model object. 16 00:00:55,670 --> 00:00:57,990 Your data can come from anywhere. 17 00:00:57,990 --> 00:01:02,220 In our app, the fact book class that we just created is the data model, and 18 00:01:02,220 --> 00:01:05,400 it consists of facts stored in an array. 19 00:01:05,400 --> 00:01:08,640 The data could also come from some other place, like the web. 20 00:01:08,640 --> 00:01:12,200 Wherever we get our data from, the model collects this data, 21 00:01:12,200 --> 00:01:15,049 performs some logic on it, and gets it ready for use. 22 00:01:16,120 --> 00:01:19,170 When we follow the MVC pattern the model does not 23 00:01:19,170 --> 00:01:21,770 communicate with the view in any way. 24 00:01:21,770 --> 00:01:25,270 This way the model object can be separated from the view and 25 00:01:25,270 --> 00:01:28,545 reused across other views without having to repeat or 26 00:01:28,545 --> 00:01:30,530 re-implement some of the same functionality. 27 00:01:31,590 --> 00:01:36,582 The views in the MVC pattern consist of everything that a user can see. 28 00:01:36,582 --> 00:01:39,000 A views purpose is to draw to the screen and 29 00:01:39,000 --> 00:01:43,525 to display data from the model object so that the user can see it. 30 00:01:43,525 --> 00:01:44,580 At, in our application, 31 00:01:44,580 --> 00:01:48,100 the views are represented by the Main.storyboard file and 32 00:01:48,100 --> 00:01:51,960 consist of our main view along with the labels and the button that we've added. 33 00:01:53,130 --> 00:01:55,990 The views are supposed to visually represent the model, but 34 00:01:55,990 --> 00:01:58,060 can't communicate with the model. 35 00:01:58,060 --> 00:02:01,370 And the model is supposed to handle the manipulation of this data for 36 00:02:01,370 --> 00:02:04,490 the view, but can't communicate with the view directly. 37 00:02:04,490 --> 00:02:07,070 This is where the controller comes in. 38 00:02:07,070 --> 00:02:09,830 The controller acts as the intermediary and 39 00:02:09,830 --> 00:02:13,740 contains all the logic to help facilitate communication between the two. 40 00:02:14,780 --> 00:02:19,490 In our app the view control class, view controller is the controller. 41 00:02:19,490 --> 00:02:22,620 So when we move the data model out of the view controller we were 42 00:02:22,620 --> 00:02:27,000 explicitly creating the model class to follow the MVC pattern. 43 00:02:27,000 --> 00:02:29,150 So why do we follow the MVC pattern? 44 00:02:29,150 --> 00:02:31,780 Well, for starters Apple really wants us to. 45 00:02:31,780 --> 00:02:35,980 The MVC design pattern is considered a core competency when developing for 46 00:02:35,980 --> 00:02:37,550 Apple's platforms. 47 00:02:37,550 --> 00:02:42,080 But more importantly following the pattern will build good programming practices, 48 00:02:42,080 --> 00:02:45,690 that become invaluable as you work with teams and on larger projects. 49 00:02:46,800 --> 00:02:49,740 Following the MVC pattern in our project also builds on 50 00:02:49,740 --> 00:02:51,870 another good programming practice. 51 00:02:51,870 --> 00:02:53,970 The single responsibility principle. 52 00:02:54,970 --> 00:02:57,990 Good object oriented design principles states, 53 00:02:57,990 --> 00:03:01,520 that every class should have a single responsibility. 54 00:03:01,520 --> 00:03:04,700 And by structuring our code as models, views and 55 00:03:04,700 --> 00:03:09,800 controllers we keep app responsibility narrowly within each class. 56 00:03:09,800 --> 00:03:13,340 Now there's some ambiguity when implementing the MVC pattern, and 57 00:03:13,340 --> 00:03:15,790 developers may do things slightly differently. 58 00:03:15,790 --> 00:03:19,310 Adhering to this pattern is best learned through practice. 59 00:03:19,310 --> 00:03:22,740 And as we learn in this project and onwards, we will talk about 60 00:03:22,740 --> 00:03:26,250 the decisions of where we implement things and how it relates to this pattern.