1 00:00:00,047 --> 00:00:04,479 In this video, we'll be refactoring our code one last time. 2 00:00:04,479 --> 00:00:07,473 That's not to say that what we've got right now is bad. 3 00:00:07,473 --> 00:00:10,420 But I'd just like to show you another way we can refactor this. 4 00:00:12,580 --> 00:00:14,260 Here's the code we started with. 5 00:00:14,260 --> 00:00:18,370 We had one ingredients fragment class and one directions fragment class. 6 00:00:18,370 --> 00:00:21,660 They were totally separate and we were repeating a lot of code. 7 00:00:22,680 --> 00:00:27,285 Then, in the last video, we refactored this to instead use only one class, 8 00:00:27,285 --> 00:00:29,250 CheckBoxesFragment. 9 00:00:29,250 --> 00:00:31,920 And in order to determine whether to show ingredients or 10 00:00:31,920 --> 00:00:34,440 directions, we pass in a Boolean, and 11 00:00:34,440 --> 00:00:39,900 then use that Boolean to grab the correct data from the recipes class. 12 00:00:39,900 --> 00:00:44,410 Now, instead of passing in a Boolean, let's recreate our ingredients and 13 00:00:44,410 --> 00:00:49,910 directions fragments, but this time let's make them extend CheckBoxesFragment. 14 00:00:49,910 --> 00:00:53,360 This way, we won't need to repeat any code in our ingredients and 15 00:00:53,360 --> 00:00:54,830 directions fragments. 16 00:00:54,830 --> 00:00:58,037 The only code they'll have is the code that makes them different. 17 00:00:59,480 --> 00:01:04,075 Let's start by getting rid of the KEY-IS_INGREDIENTS 18 00:01:04,075 --> 00:01:07,337 field in our view pager fragment class. 19 00:01:07,337 --> 00:01:10,547 And delete these two lines that show errors now, as well. 20 00:01:14,043 --> 00:01:20,368 Then, over in CheckBoxesFragment, let's delete the isIngredients variable. 21 00:01:25,979 --> 00:01:28,550 And also, the if statement that relies on it. 22 00:01:31,399 --> 00:01:37,643 Which leaves us with a contents array that never gets initialized, 23 00:01:37,643 --> 00:01:43,130 so let's set it equal to getContents and pass in our index. 24 00:01:44,950 --> 00:01:47,099 Then let's use alt+Enter to create this method. 25 00:01:51,178 --> 00:01:56,780 Now, when we extend this class, we just need to override the get contents method. 26 00:01:56,780 --> 00:02:00,750 And provide our own implementation returning whatever string array 27 00:02:00,750 --> 00:02:01,730 we'd like to display. 28 00:02:02,780 --> 00:02:06,820 But we can't extend private methods, so let's change this to public. 29 00:02:09,460 --> 00:02:12,220 And before moving on, let's think about this a little more. 30 00:02:13,290 --> 00:02:18,030 Specifically, would we ever want to create an instance of check boxes fragment? 31 00:02:19,280 --> 00:02:24,180 Probably not, it would just be an empty screen with no check boxes. 32 00:02:24,180 --> 00:02:29,910 This Default implementation of getContents is pretty useless, 33 00:02:29,910 --> 00:02:32,970 we're always going to override this method. 34 00:02:32,970 --> 00:02:36,219 And what do we call a class that can never be instantiated? 35 00:02:36,219 --> 00:02:40,198 Abstract, a little louder please, abstract. 36 00:02:40,198 --> 00:02:44,408 That's right, abstract classes cannot be instantiated. 37 00:02:44,408 --> 00:02:49,246 And since we never want this class to be instantiated, we should probably 38 00:02:49,246 --> 00:02:54,569 declare it as abstract by adding the abstract keyword between public and class. 39 00:02:57,897 --> 00:03:02,409 In addition, we also want to require that any class extending this one 40 00:03:02,409 --> 00:03:04,787 overrides the getContents method. 41 00:03:05,880 --> 00:03:08,770 If they don't override it, we won't know what to display. 42 00:03:10,040 --> 00:03:14,080 We can do this by also declaring the getContents method as abstract. 43 00:03:15,410 --> 00:03:17,797 Let's add the abstract key word after the word public. 44 00:03:20,447 --> 00:03:24,326 And then, since we don't want to provide an implementation here, 45 00:03:24,326 --> 00:03:31,034 let's get rid of the method body, And then add a semi colon. 46 00:03:31,034 --> 00:03:35,736 All right, now let's see it in action by creating a new IngredientsFragment class. 47 00:03:43,727 --> 00:03:46,531 And let's make it extend to CheckBoxesFragment. 48 00:03:49,127 --> 00:03:53,919 And if we read the error, it looks like this class either needs to be 49 00:03:53,919 --> 00:03:58,812 declared abstract or we need to implement our getContents method. 50 00:03:58,812 --> 00:04:00,978 Let's use alt+Enter, to implement the method. 51 00:04:03,045 --> 00:04:07,346 And now we just need to return the array of ingredients, just like we did earlier. 52 00:04:07,346 --> 00:04:15,403 return Recipes.ingredients[index].split("`"), 53 00:04:15,403 --> 00:04:20,173 cool, now let's do the same steps for 54 00:04:20,173 --> 00:04:23,613 our directions fragment. 55 00:04:26,869 --> 00:04:27,690 Create it. 56 00:04:31,299 --> 00:04:35,182 Make it extend CheckBoxesFragment, 57 00:04:35,182 --> 00:04:39,679 use Alt+Enter to implement getContents. 58 00:04:39,679 --> 00:04:48,915 And then return the directions for the selected recipe, Recipes.directions 59 00:04:48,915 --> 00:04:55,188 [index].split("`"). 60 00:04:55,188 --> 00:05:00,624 Finally, back in ViewPagerFragment, let's switch back to using the ingredients and 61 00:05:00,624 --> 00:05:04,967 directions fragments, instead of using two check boxes fragments. 62 00:05:17,030 --> 00:05:20,913 Then let's test the app and make sure it still works just the same. 63 00:05:35,079 --> 00:05:41,017 Nice, we've just seen two different ways to refactor the same code. 64 00:05:41,017 --> 00:05:44,318 First, we passed in an argument to a check boxes fragment. 65 00:05:44,318 --> 00:05:49,243 And second, we brought back the separate ingredients and directions fragments but 66 00:05:49,243 --> 00:05:52,640 changed them to extend a common base class. 67 00:05:52,640 --> 00:05:55,120 But which of these is the better refactoring? 68 00:05:55,120 --> 00:05:57,980 Well, for the most part, it doesn't matter. 69 00:05:57,980 --> 00:06:01,778 They both work and they both keep us from repeating ourselves. 70 00:06:01,778 --> 00:06:05,020 That said, we'll be sticking with the second refactoring, 71 00:06:05,020 --> 00:06:08,320 it does a better job at separating our concerns. 72 00:06:08,320 --> 00:06:11,035 If we want to make a change to only the directions fragment, 73 00:06:11,035 --> 00:06:13,430 it'll be easier with the second refactoring. 74 00:06:14,570 --> 00:06:17,940 Coming up, we'll see what it takes to make our app ready for tablets.