1 00:00:04,897 --> 00:00:06,060 Hey, welcome back. 2 00:00:06,060 --> 00:00:09,300 We're gonna take a short break from code to talk about the popular software 3 00:00:09,300 --> 00:00:13,680 design pattern called model-view-presenter, or MVP, for short. 4 00:00:13,680 --> 00:00:16,460 Now don't worry if this video seems a little unrelated at first 5 00:00:16,460 --> 00:00:18,050 to the app that we're working on. 6 00:00:18,050 --> 00:00:20,540 We don't need to know all the intricacies 7 00:00:20,540 --> 00:00:22,950 of the model-view-presenter design pattern. 8 00:00:22,950 --> 00:00:24,310 But the more we know about it and 9 00:00:24,310 --> 00:00:27,820 keep it in mind, the better software developers we will be. 10 00:00:27,820 --> 00:00:31,300 We don't necessarily need to follow it for smaller apps like this. 11 00:00:31,300 --> 00:00:32,670 But good programming patterns and 12 00:00:32,670 --> 00:00:37,400 practices are invaluable on larger projects or when working with teams. 13 00:00:37,400 --> 00:00:39,790 By the way, in a previous version of this course, 14 00:00:39,790 --> 00:00:42,760 we taught this as the model-view-controller design pattern. 15 00:00:42,760 --> 00:00:46,508 The two are often used interchangeably, but there are subtle differences. 16 00:00:46,508 --> 00:00:49,680 And the model-view-presenter pattern more closely aligns 17 00:00:49,680 --> 00:00:51,920 with how Android apps are architected. 18 00:00:51,920 --> 00:00:54,710 So first off, what is a software design pattern? 19 00:00:54,710 --> 00:00:55,830 When writing code, 20 00:00:55,830 --> 00:00:59,280 you'll always end up facing problems that are common to all developers. 21 00:00:59,280 --> 00:01:02,280 For example, one of those problems is how to architect our apps so 22 00:01:02,280 --> 00:01:05,000 that our components are largely reusable. 23 00:01:05,000 --> 00:01:08,480 Solutions to these common problems are known as design patterns. 24 00:01:08,480 --> 00:01:11,350 And while you weren't explicitly aware of it, we've been working with 25 00:01:11,350 --> 00:01:14,640 the model-view-presenter design pattern all along. 26 00:01:14,640 --> 00:01:16,530 Let's start with a very generic example. 27 00:01:16,530 --> 00:01:19,370 Think of a slideshow presentation you have given at work or 28 00:01:19,370 --> 00:01:23,870 in a class using a tool like PowerPoint, Google Slides, or Keynote. 29 00:01:23,870 --> 00:01:26,500 In this case, you are the presenter. 30 00:01:26,500 --> 00:01:30,810 Your job is to control what is on each slide and when they are displayed. 31 00:01:30,810 --> 00:01:34,760 These slides are being presented on a large screen, which is the view. 32 00:01:34,760 --> 00:01:37,280 This is how the audience views your material. 33 00:01:37,280 --> 00:01:41,020 Lastly, the data used to create your slides is the data model. 34 00:01:41,020 --> 00:01:44,290 Maybe your presentation is about earnings, or the results of experiments, or 35 00:01:44,290 --> 00:01:46,430 maybe your favorite episodes of Dr. Who. 36 00:01:46,430 --> 00:01:48,460 Whatever it is, you collected data and 37 00:01:48,460 --> 00:01:51,450 formatted it in some way to be viewed on the screen. 38 00:01:51,450 --> 00:01:53,940 This pattern is the same for Android apps and games. 39 00:01:53,940 --> 00:01:56,490 The model is the data that you use in the app, 40 00:01:56,490 --> 00:01:59,500 the view is how it is actually displayed on the screen, and 41 00:01:59,500 --> 00:02:02,680 the presenter manipulates the data and objects in some way. 42 00:02:02,680 --> 00:02:05,940 So next, how does this apply to our current app? 43 00:02:05,940 --> 00:02:08,790 Think for a moment about how this interactive story is set up. 44 00:02:08,790 --> 00:02:11,620 On the phone, itself, is the screen that the user interacts with. 45 00:02:11,620 --> 00:02:14,760 The layouts we create for our activities are what are shown on the screen, so 46 00:02:14,760 --> 00:02:16,720 let's call those the views. 47 00:02:16,720 --> 00:02:20,460 The layout is a presentational aspect of the app that the user views and 48 00:02:20,460 --> 00:02:21,590 interacts with. 49 00:02:21,590 --> 00:02:24,430 Now what piece of our app presents these layouts, these views? 50 00:02:24,430 --> 00:02:27,740 What part of our code manages how and when things are shown? 51 00:02:27,740 --> 00:02:30,170 And what should happen when things are tapped? 52 00:02:30,170 --> 00:02:31,980 That's right, the activities. 53 00:02:31,980 --> 00:02:35,310 These are the presenters in the MVP pattern in Android. 54 00:02:35,310 --> 00:02:35,840 As a side note, 55 00:02:35,840 --> 00:02:39,350 you will sometimes hear the presenter referred to as the controller. 56 00:02:39,350 --> 00:02:42,590 Since it controls both the view on the screen and the data model. 57 00:02:42,590 --> 00:02:45,960 We'll stick with presenter, though, as that's more widely used for Android. 58 00:02:45,960 --> 00:02:49,390 If you're interested in reading more about the exact differences between MVP and MVC, 59 00:02:49,390 --> 00:02:51,830 check the teacher's notes for some links. 60 00:02:51,830 --> 00:02:55,419 The presenter's also responsible for interacting with the data model behind 61 00:02:55,419 --> 00:02:59,510 the scenes, just like how you create and display slides in a presentation. 62 00:02:59,510 --> 00:03:02,900 Presenter is what adds, changes, or deletes data in the model 63 00:03:02,900 --> 00:03:06,000 based on what the user is doing on the screen, or the view. 64 00:03:06,000 --> 00:03:07,990 So that leaves us with the model. 65 00:03:07,990 --> 00:03:09,110 All the data in our app, 66 00:03:09,110 --> 00:03:12,920 as well as the logic around that data, goes into model objects. 67 00:03:12,920 --> 00:03:16,180 It helps to make the connection by thinking of the phrase, data model. 68 00:03:16,180 --> 00:03:18,180 Now what kind of data are we about to work with? 69 00:03:18,180 --> 00:03:20,266 That's right, in this case, it's our story. 70 00:03:20,266 --> 00:03:22,250 The data can really come from anywhere. 71 00:03:22,250 --> 00:03:25,170 We'll build it right into our app here, but it could be data from someplace 72 00:03:25,170 --> 00:03:28,880 on the web or even elsewhere on the phone, like from a gallery of photos. 73 00:03:28,880 --> 00:03:32,450 Wherever we get our data from, the model object's represented, and 74 00:03:32,450 --> 00:03:34,940 our presenter gets it ready for display. 75 00:03:34,940 --> 00:03:38,730 Presenter may even adapt the data, like when we take a long time step and 76 00:03:38,730 --> 00:03:42,950 convert it to a shortened representation that may differ from region to region. 77 00:03:42,950 --> 00:03:44,750 When we follow this MVP pattern, 78 00:03:44,750 --> 00:03:47,810 the model does not communicate with the view in any way. 79 00:03:47,810 --> 00:03:50,820 This way the model object can be separated from the view and 80 00:03:50,820 --> 00:03:53,695 reused across other views without having to repeat or 81 00:03:53,695 --> 00:03:56,240 re-implement some of the same functionality. 82 00:03:56,240 --> 00:03:59,780 This setup is the basic architecture for most Android apps. 83 00:03:59,780 --> 00:04:02,840 The core Android code that we use to develop apps has been designed in 84 00:04:02,840 --> 00:04:05,760 such a way that we're kind of forced to follow this pattern. 85 00:04:05,760 --> 00:04:08,240 The model is whatever data we need to work with. 86 00:04:08,240 --> 00:04:10,410 The views are the actual user interface of an app. 87 00:04:10,410 --> 00:04:14,450 The layouts of buttons and other things are the XML layout files. 88 00:04:14,450 --> 00:04:17,670 And the presenter is the activity where we do stuff behind the scenes and 89 00:04:17,670 --> 00:04:19,540 update the view or the data model. 90 00:04:19,540 --> 00:04:22,140 As you go further with Android development, you may even come across 91 00:04:22,140 --> 00:04:24,910 variations of these patterns that have the same basic ideas, 92 00:04:24,910 --> 00:04:28,135 but differ in some of the details for very specific reasons. 93 00:04:28,135 --> 00:04:31,310 We'll eventually talk more about those in later courses and workshops. 94 00:04:31,310 --> 00:04:35,475 But understanding the basic MVP architecture is some key groundwork. 95 00:04:35,475 --> 00:04:39,855 All right, so that tells us a bit about what MVP is and how it's used in our apps. 96 00:04:39,855 --> 00:04:42,325 But we're still missing the why behind it. 97 00:04:42,325 --> 00:04:44,465 Don't worry, though, that's coming up next in the next video.