Refactor - Again?6:18 with Ben Deitch
In this video we'll refactor IngredientsFragment and DirectionsFragment... Again!
In this video, we'll be refactoring our code one last time. 0:00 That's not to say that what we've got right now is bad. 0:04 But I'd just like to show you another way we can refactor this. 0:07 Here's the code we started with. 0:12 We had one ingredients fragment class and one directions fragment class. 0:14 They were totally separate and we were repeating a lot of code. 0:18 Then, in the last video, we refactored this to instead use only one class, 0:22 CheckBoxesFragment. 0:27 And in order to determine whether to show ingredients or 0:29 directions, we pass in a Boolean, and 0:31 then use that Boolean to grab the correct data from the recipes class. 0:34 Now, instead of passing in a Boolean, let's recreate our ingredients and 0:39 directions fragments, but this time let's make them extend CheckBoxesFragment. 0:44 This way, we won't need to repeat any code in our ingredients and 0:49 directions fragments. 0:53 The only code they'll have is the code that makes them different. 0:54 Let's start by getting rid of the KEY-IS_INGREDIENTS 0:59 field in our view pager fragment class. 1:04 And delete these two lines that show errors now, as well. 1:07 Then, over in CheckBoxesFragment, let's delete the isIngredients variable. 1:14 And also, the if statement that relies on it. 1:25 Which leaves us with a contents array that never gets initialized, 1:31 so let's set it equal to getContents and pass in our index. 1:37 Then let's use alt+Enter to create this method. 1:44 Now, when we extend this class, we just need to override the get contents method. 1:51 And provide our own implementation returning whatever string array 1:56 we'd like to display. 2:00 But we can't extend private methods, so let's change this to public. 2:02 And before moving on, let's think about this a little more. 2:09 Specifically, would we ever want to create an instance of check boxes fragment? 2:13 Probably not, it would just be an empty screen with no check boxes. 2:19 This Default implementation of getContents is pretty useless, 2:24 we're always going to override this method. 2:29 And what do we call a class that can never be instantiated? 2:32 Abstract, a little louder please, abstract. 2:36 That's right, abstract classes cannot be instantiated. 2:40 And since we never want this class to be instantiated, we should probably 2:44 declare it as abstract by adding the abstract keyword between public and class. 2:49 In addition, we also want to require that any class extending this one 2:57 overrides the getContents method. 3:02 If they don't override it, we won't know what to display. 3:05 We can do this by also declaring the getContents method as abstract. 3:10 Let's add the abstract key word after the word public. 3:15 And then, since we don't want to provide an implementation here, 3:20 let's get rid of the method body, And then add a semi colon. 3:24 All right, now let's see it in action by creating a new IngredientsFragment class. 3:31 And let's make it extend to CheckBoxesFragment. 3:43 And if we read the error, it looks like this class either needs to be 3:49 declared abstract or we need to implement our getContents method. 3:53 Let's use alt+Enter, to implement the method. 3:58 And now we just need to return the array of ingredients, just like we did earlier. 4:03 return Recipes.ingredients[index].split("`"), 4:07 cool, now let's do the same steps for 4:15 our directions fragment. 4:20 Create it. 4:26 Make it extend CheckBoxesFragment, 4:31 use Alt+Enter to implement getContents. 4:35 And then return the directions for the selected recipe, Recipes.directions 4:39 [index].split("`"). 4:48 Finally, back in ViewPagerFragment, let's switch back to using the ingredients and 4:55 directions fragments, instead of using two check boxes fragments. 5:00 Then let's test the app and make sure it still works just the same. 5:17 Nice, we've just seen two different ways to refactor the same code. 5:35 First, we passed in an argument to a check boxes fragment. 5:41 And second, we brought back the separate ingredients and directions fragments but 5:44 changed them to extend a common base class. 5:49 But which of these is the better refactoring? 5:52 Well, for the most part, it doesn't matter. 5:55 They both work and they both keep us from repeating ourselves. 5:57 That said, we'll be sticking with the second refactoring, 6:01 it does a better job at separating our concerns. 6:05 If we want to make a change to only the directions fragment, 6:08 it'll be easier with the second refactoring. 6:11 Coming up, we'll see what it takes to make our app ready for tablets. 6:14
You need to sign up for Treehouse in order to download course files.Sign up