ese methods, right? 3:19 And because we explicitly said that we want all the methods exposed through 3:24 inheritance, we have to make sure that they work the way that we want them to. 3:29 After looking at this in more detail, I'm not sure we want all this stuff exposed. 3:34 So let's take a look at the composition approach. 3:38 The basic concept is just to re-use code that you need and 3:41 encapsulate the fact that you're using it at all. 3:44 Here, let's take a look. 3:46 So first we create our class. 3:48 It doesn't extend anything, it's just a normal class. 3:49 it's going to have a private field that must implement the blocking 3:52 queue interface of items, we'll call it items. 3:55 Blocking queue is the interface that helps us keep track of the max number of items. 3:59 Now, remember we want to declare with the interface so 4:04 that we can choose whatever specific implementation we want later. 4:07 So now we'll add our constructor. 4:11 And here, 4:14 we want to choose the implementation for the [SOUND] blocking queue. 4:14 Array blocking queue. 4:18 In the constructor, 4:20 we'll require the creator to specify the maximum number of items. 4:21 Then we'll use that to instantiate the instance and 4:26 choose the implementation array blocking queue. 4:29 And pass in the max items allowed. 4:32 Note that I used the diamond operator, 4:35 no need to redefine that it's a blocking queue of items. 4:37 So, we know that we want to be able to check the bin as empty. 4:41 So let's do it. 4:45 See how we're only exposing the methods that we want, and 4:46 we can also name them any way we want. 4:49 So we can add specific terms too, right? 4:52 Like so each of these spaces in the bins are called slots. 4:54 [SOUND] So, why not get available slots? 4:57 But check out this restock method. 5:01 [SOUND] This replaces the add method that we had from the inherited stock. 5:03 See how that bins API is pretty much exactly what we mean, 5:07 while using basically the same tricks. 5:10 And remember, because we've encapsulated our queue by making it private, 5:13 we can swap that out whenever, as long as the public methods still do and 5:17 return what they're expected to. 5:20 So there you go. 5:23 Best practices are always going to tell you to favor composition over inheritance. 5:24 And I hope you can start to see why. 5:28 Inheritance is basically saying bin is a queue, and 5:31 composition is saying the bin has a queue. 5:35 In my experience, I'd suggest that you start by composing and 5:39 move to inheritance if you end up writing all of the methods. 5:43 So you might be wondering, this is all great Craig, but 5:47 what does this have to do with testing? 5:50 Well, remember that mantra of testing that I keep repeating? 5:51 Testing enforces best practices. 5:55 Composition is one of those best practices. 5:57 Let's take a quick break and 6:00 I'll show you why composition is a great testing solution and how it helps solve 6:01 another programming problem and helps bring forward another best practice. 6:05 The single responsibility principle. 6:09
You need to sign up for Treehouse in order to download course files.Sign up