Simple Refactoring: Using a New Class3:47 with Ben Deitch
We have a new object in our project; let's see how to use it in our existing code!
Things to Consider
First, we need to instantiate a fact book object. 0:00 We'll do it similarly to how we created a new random object in the fact book class. 0:03 We'll use this new fact book object to do the work of getting us a random fact 0:08 when the button is clicked. 0:12 We create it here in the on click method. 0:14 But if we do that, 0:17 a new fact book object would be created every time we tap on the button. 0:18 That doesn't seem very efficient. 0:23 What if our object was huge, with thousands of facts to choose from? 0:24 On each tap, 0:28 we'd be wasting tons of processing time just in recreating the fact book. 0:29 Instead, let's create it just once as a property. 0:34 Up here before the on create method but after we define our class, 0:38 let's add some space and type private val, and name it factbook. 0:42 And notice that we're using the private keyword word. 0:50 We only want this variable to be available inside this class. 0:52 No other code outside our activity needs to know about it. 0:57 Next we need to initialize our fact book. 1:01 Type equals capital, 1:03 capital F, A, and then use autocomplete to finish it. 1:08 And finally, add the parenthesis. 1:13 This creates a new fact book object using the default constructor. 1:16 But wait, we didn't add a constructor to our fact book class. 1:21 And luckily, we don't need to. 1:24 If we don't provide our own constructor, 1:26 Catlin will automatically create one behind the scenes. 1:28 So even if we don't add a constructor to a class, 1:32 we can always create objects from that class by using the default constructor. 1:34 Back in our own quick listener, let's get a random fact from our fact book object 1:40 and store it in a variable name fact. 1:44 Type vowel, fact, and 1:47 set it equal to fac, book, dot and cool. 1:50 Our gift fact method is right at the top of auto complete. 1:55 But where did the rest of these methods come from? 1:58 The ones we didn't write? 2:00 In Catlin, every class either directly or indirectly extends from the any class. 2:02 Even though we didn't use the extends keyword when writing our factBook class, 2:09 it still extends the any class by default. 2:12 So these other methods come from the any class. 2:15 Let's hit Enter, and we're done. 2:18 Now let's take a quick review of our code. 2:21 We create our activity, and give it the layout we'd like it to display. 2:23 Then we initialize our view variables to the views in the layout 2:27 by using their IDs. 2:30 Then we set an on click listener for our button, and when the button is tapped, 2:32 we use our fact book object to get a random fact and update the fact text view. 2:36 This looks pretty good, but let's take one more pass at our fact book class. 2:41 First we see one of the main problems with using comments. 2:46 We ideally want to write code that's easy to understand without additional comments. 2:50 Comments tend to be forgotten about after they're first written and 2:54 can end up in not making any sense. 2:57 Like this one, let's delete it. 3:00 All right there's one more thing we should change here, can you guess what it is? 3:04 One of the properties of our fact book is all the facts it contains, so 3:09 this facts array should really be a property of our fact book and 3:13 not a local variable of the get fact method. 3:17 Let's cut the facts declaration from the get getFact method and 3:20 then paste it up here under Properties. 3:25 Awesome, now it's time to test the app. 3:30 Remember, the goal of refactoring is to make changes to the code 3:34 without changing the behavior of the app. 3:37 So it should work exactly the same as before. 3:40 Great work. 3:45
You need to sign up for Treehouse in order to download course files.Sign up