Encapsulation6:39 with Jeremy McLain
Hide implementation details and make objects easy to use with encapsulation.
Another place we'll want to use arrays is to create a path for 0:00 invaders to move down. 0:03 We can represent the path in code as an array of map locations. 0:05 Starting at index zero in the array, 0:09 invaders can move down the path by moving to the next index in the array. 0:11 To start, let's create an array of map locations here in Main. 0:17 This will create a path that moves from the left to right across 0:20 the middle of the map. 0:23 So we've already got our mapLocation variable here. 0:25 So let's take this out and put our square brackets here. 0:29 We'll call it path, and opening closing curly braces, 0:33 we'll use the shortcut method here. 0:37 And then we'll create a new MapLocation 0:40 starting at x equals 0 and y equals 2, so 0:45 that it's going down the middle of the map. 0:50 And we'll pass in the map so that we can validate that it's on the map. 0:55 Now we need to create seven more of these, so I'll just copy and 1:00 paste here till I got eight total. 1:05 So, let's see here. 1:07 One, two, three, four, five, six, seven, eight. 1:09 Okay, now, 1:12 we need to make the path move from the left to the right side of the map. 1:13 So starting at zero, we'll go to, so 1:18 this is x equals 0, then we'll go to x equals 1, 1:22 x equals 2, x equals 3, x equals 4, 5, 6, and 7. 1:26 All right. So now we've got an array of map locations 1:31 that represents our path going down the middle of the map. 1:35 Even though we've named this array of map locations path, 1:41 it's still just an array of map locations. 1:44 It isn't a path object. 1:46 This is the data that specifies where the path is on the map. 1:49 We really should wrap this data in a class that models not only where the path is, 1:53 but also how the path is used. 1:58 That brings us to the next core principle of object-oriented programming, 2:01 encapsulation. 2:06 Encapsulation helps to define what an object is while hiding how it works. 2:08 A good example of encapsulation in the physical world is a car. 2:13 Cars are complex machines with lots of moving parts inside. 2:18 However, you don't really need to know all about these parts in order to drive a car. 2:21 You only need to know how to use the steering wheel, gas pedal, 2:26 brake pedal, and gear shifter. 2:30 Everything else is hidden from view as much as possible. 2:32 Much of it is under the hood, if you will. 2:36 What's nice about this system us that even though different cars 2:39 might have different engines, I can still get in any car and drive it. 2:43 All this makes driving a car a lot easier. 2:48 And by making the car easier to drive, it's also safer to use. 2:50 We want to make our classes easy to use. 2:55 So when writing a class, we need to make sure 2:57 that we're exposing only what's absolutely necessary to use the class. 3:00 This will help people to know how to use the class and 3:04 help them avoid accidentally misusing it. 3:07 Encapsulation is used to hide implementation details and 3:11 restrict what users of objects can do. 3:14 Back to our path example. 3:18 Once a path is created, it shouldn't change. 3:19 Paths in the real world don't just get up and move. 3:22 Right now we have our path implemented as an array of map locations. 3:26 The array class provides lots of methods for manipulating arrays. 3:30 But we don't need all of that. 3:35 We can restrict what can be done with our array of map locations by encapsulating it 3:37 or wrapping it in a class, and 3:42 then expose those small sort of operations that can be done with the path. 3:44 Back in code, let's put together our path class. 3:49 Let's start by writing the constructor. 3:53 When writing the constructor, we need to think about the absolute 3:55 bare minimum amount of information that every Path object needs. 3:58 That becomes the parameters to the constructor. 4:03 I'd say it's the locations on the map that the path uses. 4:06 We can pass those in as an array of map locations. 4:10 We'll store these locations as a field in the class, 4:16 only this time we aren't going to let users of this class access them directly. 4:19 So instead of typing public here like we've done with other fields, 4:25 we'll type private. 4:29 We're going to store this as a MapLocation array. 4:31 And we'll call it _path. 4:34 Notice that I prefixed the field name with an underscore. 4:38 We only use it for the names of private fields. 4:41 It isn't required, and you may see other code that doesn't have this underscore. 4:44 This is a common convention in C# though. 4:48 It's helpful for distinguishing between instance variables and method variables. 4:50 If we didn't have this underscore, we'd either have to name these two variables 4:55 differently, oop, put my semicolon there, or 4:59 we'd have to type this.path = path. 5:04 So this refers to the current object's .path, 5:09 which is the current object's field path, and assigns 5:13 the path variable that's being passed in to this.path. 5:19 But because we're using the underscore, we'll just say _path equals path. 5:23 And that gives us the same effect. 5:32 You can decide whether or not you want to use this convention. 5:34 When programming at a company, there will often be a document called a coding 5:37 standard which specifies how to name different types of variables. 5:41 That way, everyone working on the code uses the same conventions. 5:46 We should also make sure that the path field isn't accidentally 5:50 overwritten by code that's within the path class. 5:53 So we'll type readonly here. 5:56 The thing you should know about readonly is that it only prohibits 6:00 overriding the field with a different value. 6:04 It doesn't prohibit anyone from changing the individual items in the array. 6:07 That's one of the reasons we're wrapping this array in a class in the first place. 6:12 We've now used encapsulation to hide the array of map locations. 6:17 By wrapping the array in a class and using private to restrict access, 6:21 there's no way for users of this class to alter the path. 6:25 In fact, users of this class can't do much of anything. 6:29 Next we'll start to open it up by adding behaviors we need the path class to have. 6:33
You need to sign up for Treehouse in order to download course files.Sign up