Abstraction at Play8:34 with Craig Dennis
You can create computed properties that help to abstract away how your object is working internally.
One nice thing that we can do as proactive developers, 0:01 is to add methods that provide functionality or 0:04 information that we expect might be a common question about our object. 0:06 By gathering common functionality, we can help to reduce code duplication for 0:11 ourselves, and for consumers of our code. 0:15 We can use methods to provide conceptual state 0:18 about our instance that other developers, and 0:21 us included, don't actually need to worry about when using the object. 0:23 So more specifically, if we try and 0:27 visualize how people are going to use our Pez dispenser object, 0:30 one common question I could foresee happening would be, is this thing empty? 0:33 Now I'll check, and I'll say no, because there is indeed some in there. 0:38 Because that is probably going to be asked of the object, 0:41 we should probably provide the answer to that common question to our users. 0:44 What we don't want to do is force the user to understand how our 0:49 object works internally. 0:52 We just want them to be able to ask, is it empty? 0:54 And our object can respond with yep or nope. 0:57 So let's do that. 0:59 Let's create that method but before we create that method, let's jump back into 1:01 Jshell and add some more powerful tools to the expressions we write. 1:04 Okay, so go ahead and open up JShell, and let's walk through a couple things. 1:09 So first, we know how to see if a number is greater or 1:13 less than another number, right? 1:18 So we can do that, we can say 5 is greater than 4, and we use the sign like that. 1:19 And we can also say that 6 is less than 7. 1:24 Now one thing that might not be too clear is equality of numbers. 1:28 So, let's go ahead and let's say that five is equal to five. 1:32 Now, there's a little thing that you need to be careful of here. 1:37 When you're testing equality, you need to use double equals. 1:40 So you say equal equal five. 1:43 And that is because, as you know, 1:45 the equal sign has already been used for variable assignments, like this, right. 1:48 So, if we said the age is equal to 19, 1:54 that's how we set the age for variable assignment. 1:57 But, if we wanted to check the age, we would need to do double equals. 2:00 Otherwise, we'd just be overwriting it each time. 2:03 Common mistake, pay attention to that one. 2:05 Just want to make sure that was out there, so we have this now we can check equality. 2:08 So, we should be able to make the is empty method, right? 2:12 Since we don't want anybody else to have to write the double equals code 2:15 double equals zero. 2:19 We definitely know that we want the method to be public, 2:20 because we want people to use this. 2:24 So we'll say public, and next we want it to return a true or 2:25 false value, which means we'll be expecting a boolean. 2:30 Booleans are a special case, when you name property methods. 2:34 So the standard, you know, for getters like we have before, we have a get. 2:38 If you have one that is a boolean, you want it to be is, 2:42 because it kind of reads better that way, and you'll see here a second. 2:45 So we'll say IS empty, right? 2:48 Okay, so we'll do that, we'll open it, we'll close it. 2:52 So, what should we do? 2:55 Let's think, we could create a brand new private boolean field and 2:57 store it up here, right? 3:01 But with our current code, whenever somebody does this fill method here, 3:02 we'd have to set it to true or false. 3:06 And it seems like we'd have to like always keep state in that way. 3:09 It seems awfully brittle doesn't it? 3:12 It's a whole lot to remember and if we forget, 3:14 it could actually end up doing more harm than good. 3:16 So a nice thing about objects is that we can actually compute our property. 3:19 Now, we know how to do it, right. 3:24 We just talked about it. 3:26 If the Pez count is zero, then it's empty. 3:27 So we could do something like this, right. 3:31 So this code is going to run. 3:33 So we can make a new variable here, 3:34 and we'll name it something different, is actually empty. 3:36 And we'll do just like we did down here in the Jshell. 3:40 We'll say if the Pez count is double equal to zero, 3:43 the Pez count is equal to zero, then it is empty, right? 3:48 So, we'll return this variable here is actually empty. 3:51 That works. 3:56 But wait a second. 3:57 Notice how we're creating a variable here, 3:59 in the just immediately returning its value. 4:02 Well, we can actually clean this up. 4:04 We can actually return this expression. 4:06 So I'm gonna copy that and I'm going to paste it here. 4:07 And then I want to get rid of this line here and so 4:11 it just has this has pezCount is double equal to zero. 4:13 Because this is going to return a true, and then our method is going to return 4:16 that value and it's gonna be a boolean, or a true or a false. 4:19 Cool. So I have saved that and 4:23 it looks like it's working. 4:24 I wanna go ahead. 4:25 Let's make sure it is. 4:26 So we're gonna explore. 4:27 We'll open PezDispenser to that Java. 4:29 We'll make a new PezDispenser. 4:34 You know use that poor naming convention there of PD, we'll say new PezDispenser. 4:37 Cool so we've got a new one, 4:45 I'm going to press control L, get a new screen here, all right. 4:48 So it was just created. 4:51 So isEmpty best return true, right? 4:53 Because the pezCount comes in, it's set to zero. 4:55 So let's do that. 4:58 Cool, so it is empty, and then, if we fill it. 5:01 So, that's going to set this max_pez on the pezCount. 5:07 So now the pezCount should be max_pez, which is 12. 5:10 So lets see if pd.isEmpty, and it should return false. 5:13 Cool. It's working. 5:19 So since we've explored, and we know that it works, 5:21 let's write out some code over here in our example class that uses it. 5:24 So, let's test out some if statements. 5:28 Now remember, if statements allow us to run certain code 5:33 only if certain expressions are true. 5:35 So, let's just play with it a little bit here. 5:37 We'll say, 5:40 if dispenser is empty, 5:43 then we'll print out 5:48 dispenser is empty. 5:52 So here, let's show that the dispenser is no longer empty. 6:02 So, we want to do the inverse of empty. 6:06 Now, we don't need to write that method, because we have the capability to do that. 6:09 We can use the not symbol. 6:13 Not inverts the expression, or negates the value it follows. 6:15 So, we do something like this. 6:18 So if an exclamation point is not, 6:20 if not the dispenser is empty, 6:27 [SOUND] we'll print out, 6:33 Dispenser is full. 6:38 Cool, so let's drop out of JShell, 6:42 and now if we compile and run this, here we go. 6:45 So we see the dispenser is empty and 6:50 we know that that happened because this will only print out if it was empty. 6:51 And then, this will only print out if it's full, and on both sides it prints out. 6:55 We did it. 6:59 Awesome, way to be proactive. 7:01 Did you see how by exposing that most likely common request about 7:03 the state of our object, we have avoided the need for us and 7:07 other developers to write that exact same equality check code again and again? 7:10 We encapsulated how the check happens and 7:14 we have avoided having to actually show how many Pez were available. 7:17 Chalk that up as another added benefit to using objects. 7:21 If you look at the code in example dot Java, 7:25 you see how much it reads almost like English. 7:27 If the dispenser is empty, fill it. 7:30 In my opinion, this is the key to being a successful programmer. 7:33 The more you can write your code so that someone can just look at it and 7:37 understand what it's doing, the more you are mastering the art of programming. 7:40 This will become increasingly more natural the more you practice. 7:44 And the more you get to know your object. 7:48 And you witness how it's being used, the more you'll be able to make 7:50 sure that the methods you provide are clear and readable. 7:53 The other important thing that I'd really like to emphasize here is this. 7:56 Try not to take offense if someone doesn't understand what your code is 8:00 trying to accomplish. 8:03 Always try to take that as an opportunity to further 8:05 improve your code for readability. 8:08 Now, chances are, 8:10 if that person didn't understand your code, others might have the same problem. 8:11 After all, the main reason that we're writing code is to express our thoughts. 8:15 We definitely want them to be understood. 8:19 The more legible your code is, the more it can be reused and maintained. 8:21 Always strive for clarity. 8:25 The time spent is well worth it, I promise. 8:27 All right, so after this next exercise, 8:30 let's start getting some candy out of this. 8:31
You need to sign up for Treehouse in order to download course files.Sign up