1 00:00:00,025 --> 00:00:06,821 [NOISE] So let's keep moving on our project. 2 00:00:06,821 --> 00:00:11,110 I find that working on the model is a great way to shake out a prototype. 3 00:00:11,110 --> 00:00:13,910 So let's think through our problem a bit. 4 00:00:13,910 --> 00:00:17,340 We obviously need to have an idea, which has a title. 5 00:00:17,340 --> 00:00:19,440 And the idea'll also have a creator. 6 00:00:19,440 --> 00:00:22,290 Now, we'll worry about how to keep track of votes in a bit. 7 00:00:22,290 --> 00:00:25,948 First, let's create the ability to add ideas and then retrieve them. 8 00:00:25,948 --> 00:00:29,925 Now, to me, that sounds like we need to store things somewhere. 9 00:00:29,925 --> 00:00:34,450 I mean, what we wanna do is make it so that even if our web server restarts, 10 00:00:34,450 --> 00:00:37,730 that our data is saved someplace, or persistent. 11 00:00:37,730 --> 00:00:40,010 That's a little out of scope for this project, though, so 12 00:00:40,010 --> 00:00:42,030 check the teacher's notes for more info. 13 00:00:42,030 --> 00:00:43,110 So let's do this. 14 00:00:43,110 --> 00:00:44,600 Let's use an interface and 15 00:00:44,600 --> 00:00:48,770 we'll build a prototype implementation that just uses normal data structures. 16 00:00:48,770 --> 00:00:49,660 Sound good? 17 00:00:49,660 --> 00:00:53,020 That way, later we can swap out a database implementation or 18 00:00:53,020 --> 00:00:53,910 anything for that matter. 19 00:00:53,910 --> 00:00:57,380 As long as it meets our contract, we'll be good to go. 20 00:00:57,380 --> 00:01:01,370 That seems like a pretty light way to do things in its micro framework land. 21 00:01:01,370 --> 00:01:05,420 Now, larger frameworks provide some of this capability out of the box, but our 22 00:01:05,420 --> 00:01:09,650 micro framework here doesn't necessarily force us into any direction at all. 23 00:01:09,650 --> 00:01:13,855 So what do you say we drop that contract. 24 00:01:13,855 --> 00:01:17,120 So let's make a new class. 25 00:01:17,120 --> 00:01:18,700 And we're gonna put it in a new package. 26 00:01:19,930 --> 00:01:28,648 So we're gonna say model.Courseidea. 27 00:01:28,648 --> 00:01:32,840 And this is where we will store the data structure basically. 28 00:01:32,840 --> 00:01:39,440 So we said that this is gonna have a string [BLANK AUDIO] called title, 29 00:01:39,440 --> 00:01:45,510 and we also said it's gonna have a private string called creator. 30 00:01:45,510 --> 00:01:50,825 So let's go ahead and we'll generate, remember that was Cmd+N, 31 00:01:50,825 --> 00:01:54,740 we're gonna generate the constructor. 32 00:01:54,740 --> 00:01:56,070 And then, we'll take both of those. 33 00:01:57,130 --> 00:02:01,870 Title and creator and while we're at it, let's do another generation. 34 00:02:01,870 --> 00:02:05,476 Let's generate some getters. 35 00:02:10,424 --> 00:02:16,594 And while we're here, let's go ahead and let's generate the equals and hashCode. 36 00:02:16,594 --> 00:02:17,991 I'll just use the default one. 37 00:02:20,225 --> 00:02:21,313 Great. 38 00:02:22,468 --> 00:02:25,010 Made you type all that stuff out? 39 00:02:25,010 --> 00:02:26,859 Sorry about that. 40 00:02:26,859 --> 00:02:31,354 Now, the simplest way to get where we're headed is to have a single interface that 41 00:02:31,354 --> 00:02:34,010 accesses all we need data storage wise. 42 00:02:34,010 --> 00:02:36,370 So let's build it and we'll talk about it. 43 00:02:36,370 --> 00:02:40,720 So a commonly recognized way of naming the way that you plan to access the data 44 00:02:40,720 --> 00:02:44,960 is with the acronym DAO, or Data Access Object. 45 00:02:46,600 --> 00:02:49,990 This is a design pattern that's used to abstract a way how you retrieve and 46 00:02:49,990 --> 00:02:50,980 store data. 47 00:02:50,980 --> 00:02:52,420 Check the teacher's notes for more on this. 48 00:02:52,420 --> 00:02:55,140 So what we wanna do is in our new model, here, 49 00:02:55,140 --> 00:03:00,120 we wanna make a new Java Class and we're gonna change it to be an Interface. 50 00:03:01,210 --> 00:03:06,202 And what we're gonna make that 51 00:03:06,202 --> 00:03:11,196 called is the CourseIdeaDAO, 52 00:03:11,196 --> 00:03:14,661 Data Access Object. 53 00:03:14,661 --> 00:03:21,798 So all we really need for this is a way to add new ideas. 54 00:03:21,798 --> 00:03:28,530 So we wanna make something called add and it will take a CourseIdea, 55 00:03:28,530 --> 00:03:33,310 called an idea, and then we also need to have a way to find them. 56 00:03:33,310 --> 00:03:34,180 So let's do that. 57 00:03:34,180 --> 00:03:40,025 Let's make a list of CourseIdeas 58 00:03:40,025 --> 00:03:43,890 and it's returned from a method that's called findAll. 59 00:03:43,890 --> 00:03:45,880 Now note, because this is the interface, 60 00:03:45,880 --> 00:03:47,950 we aren't writing any of the implementation. 61 00:03:47,950 --> 00:03:51,790 We're gonna do that now, so let's add our silly implementation. 62 00:03:53,030 --> 00:03:56,050 So let's make a new class in this model here. 63 00:03:57,170 --> 00:04:04,950 Make a new Java Class and we're going to call it SimpleCourseIdeaDAO. 64 00:04:04,950 --> 00:04:09,390 Now, i'm calling it simple just to make sure that we remember that this is 65 00:04:09,390 --> 00:04:12,450 a simple way of doing things, this is not how we should actually do it. 66 00:04:12,450 --> 00:04:14,052 So let's do this. 67 00:04:14,052 --> 00:04:18,351 And what we want it to do is wanna implement that 68 00:04:18,351 --> 00:04:21,770 Data Access Object interface. 69 00:04:21,770 --> 00:04:24,198 So that's the CourseIdeaDAO. 70 00:04:24,198 --> 00:04:25,884 Right away, it's gonna start complaining, and 71 00:04:25,884 --> 00:04:28,300 it's gonna say that we need to implement the methods, so let's do it. 72 00:04:29,480 --> 00:04:30,864 We'll implement both of these guys. 73 00:04:35,990 --> 00:04:41,286 Then, we're gonna need to have a private list of ideas that will maintain, 74 00:04:41,286 --> 00:04:45,700 cuz we're gonna do this all using normal data structures. 75 00:04:45,700 --> 00:04:48,020 We're not gonna be using a database. 76 00:04:48,020 --> 00:04:52,161 So we'll put here, we'll put private 77 00:04:52,161 --> 00:04:57,940 List and we'll call those ideas. 78 00:05:00,510 --> 00:05:05,789 And on the constructor, [BLANK AUDIO] we 79 00:05:05,789 --> 00:05:11,787 will automatically just create a new ArrayList. 80 00:05:11,787 --> 00:05:19,510 [INAUDIBLE] So we'll say ideas = new ArrayList. 81 00:05:20,730 --> 00:05:21,230 Awesome. 82 00:05:22,660 --> 00:05:25,385 And it's using the diamond method. 83 00:05:25,385 --> 00:05:28,201 So now we just kinda do what we would think. 84 00:05:28,201 --> 00:05:35,070 So if somebody calls add, we wanna add our idea to our list. 85 00:05:36,920 --> 00:05:42,660 And then, just in case somebody tries to get cheeky and does a findAll and 86 00:05:42,660 --> 00:05:47,770 appends the list, let's go ahead and make sure that we make a copy of the list. 87 00:05:47,770 --> 00:05:49,990 So we're gonna say new ArrayList. 88 00:05:51,830 --> 00:05:56,190 And remember, collections can take another collection and it makes a new one from it. 89 00:05:56,190 --> 00:05:59,730 So this is not actually a reference to the list. 90 00:05:59,730 --> 00:06:03,890 It is a brand new list, so you can't append this. 91 00:06:03,890 --> 00:06:08,565 You can't do findAll and then do your own appending to it, make sense? 92 00:06:08,565 --> 00:06:13,100 And now for the part that's pretty crazy/cool, ready? 93 00:06:13,100 --> 00:06:16,170 So we're gonna flip back to the app Main. 94 00:06:17,740 --> 00:06:21,800 And in here, remember we're inside of a main method here. 95 00:06:21,800 --> 00:06:26,760 So let's go ahead and make our model appear. 96 00:06:26,760 --> 00:06:34,300 So we're gonna say CourseIdeaDAO, and we're gonna call that dao. 97 00:06:34,300 --> 00:06:40,410 So remember, this CourseIdeaDAO is our Interface. 98 00:06:40,410 --> 00:06:42,290 And this is our implementation. 99 00:06:42,290 --> 00:06:44,500 So we're gonna implement the SimpleCourseIdeaDAO. 100 00:06:44,500 --> 00:06:48,820 Now, later if we wanted to, we would switch that out with the database one. 101 00:06:48,820 --> 00:06:54,870 So any one of these route methods can now close over this DAO. 102 00:06:54,870 --> 00:06:56,220 Does that make sense? 103 00:06:56,220 --> 00:07:00,870 Now, I want to remind you this should only be used for prototyping. 104 00:07:00,870 --> 00:07:02,350 Do not do this at home. 105 00:07:02,350 --> 00:07:08,170 This SimpleCourseIdeaDAO will not survive a server restart. 106 00:07:08,170 --> 00:07:10,200 We'll talk about more of it here in a bit. 107 00:07:10,200 --> 00:07:14,250 But remember it's called simple, don't do it at home. 108 00:07:14,250 --> 00:07:18,440 Please, please, please remember that this is just a prototype. 109 00:07:18,440 --> 00:07:20,248 Do not run this live. 110 00:07:20,248 --> 00:07:24,670 That simple CourseIdea, data access object, is goofy and 111 00:07:24,670 --> 00:07:26,520 won't survive a server restart. 112 00:07:26,520 --> 00:07:30,310 You really wanna persist or save this data for later use. 113 00:07:30,310 --> 00:07:32,960 And our implementation is really cutting corners. 114 00:07:32,960 --> 00:07:34,930 So after we use this a bit, you will for 115 00:07:34,930 --> 00:07:37,560 sure want to implement a data based implementation, or 116 00:07:37,560 --> 00:07:42,500 even a file based implementation, just so you don't need to keep re-adding things. 117 00:07:42,500 --> 00:07:44,190 That said though, let's go ahead and 118 00:07:44,190 --> 00:07:46,300 add the ability to add, right after this break,