This workshop will be retired on May 31, 2020.
Issuing Landing Instructions5:49 with Pasan Premaratne
Now that we've implemented both runway and terminal logic, let's tie it all together in the land method of the ControlTower
We've implemented the runway, terminal, and gate lodging. 0:01 So let's tie it all together in land method of the control tower, 0:04 which is down here. 0:08 Earlier, when we implemented the logic for the terminal enum, we simply returned 0:11 enum members with the associated values set to nil as a temporary workaround. 0:16 So let me actually scroll back up to Terminal for a second. 0:21 And over here, you can see we're simply returning A and nil values, 0:24 no actual gate numbers. 0:27 To fix this, now we can go back in and enter a couple lines of code for 0:29 each case in the switch statement. 0:33 So if it's a domestic airline and more specifically, 0:35 it's a Delta flight, let's get a gate for Terminal A. 0:38 So we could say let gate = GateManager.sharedInstance.gateFor and 0:42 we'll say Terminal A. 0:52 Now this returns a integer value as the gate, so we can use that in here. 0:56 When we specify the terminal for the GateManager's instance method, 1:02 we just pass in nil for the associated value to satisfy the compiler. 1:06 We don't really have a gate number at this point. 1:10 With the gate value we get back, 1:13 we assign this as an associated value to the relevant enum member and return it. 1:14 So let's repeat this process for every airline. 1:20 So we'll say let gate, I can just copy this. 1:23 And, let me go change everything so 1:34 this is B, this is C, this is International, 1:39 and the last one is Private. 1:46 And then finally, we need to use that gate as an associated value. 1:53 Now you could modify the gateFor terminal that's this method over here 2:01 you could modify this method to return a terminal value instead of just an integer. 2:06 That should allow you to clean up the terminal logic as well and 2:13 eliminate a lot of repeated code. 2:16 Now, I haven't done that specifically, 2:19 because I want you to try it out as a refactoring exercise. 2:21 No guidance, just try and improve this code on your own. 2:25 Okay, with that done, let's head back down to the land method. 2:29 We already have the runway over here, so all we need is the terminal. 2:33 So we can say let terminal=Terminal.terminal and 2:38 we'll pass in the airline, super simple. 2:44 The airline protocol encodes the type information and we can cast the type to 2:48 a more specific type and figure out things, all of this we've already done. 2:53 So let's add a terminal store property to the landing instruction struct now that we 2:58 have it, so that we can return it. 3:03 So we'll say let terminal, and just like the type for 3:04 runway, since this is a nested type, we'll say ControlTower.terminal. 3:09 Okay, let's go back down. 3:15 There's an error and that's because I need to return the terminal. 3:16 Take a step back and inspect your code. 3:22 Make sure you really understand things because 3:25 we've pretty much implemented all the logic for our control tower 3:27 by relying on the information encoded in the Airline protocol. 3:31 We do have enums that indicate what different airlines we can expect to land, 3:35 but we didn't need to create an instance of, 3:41 say, domestic airline to get this to work. 3:43 But finally, with all of this out of the way, 3:46 let's now focus on creating the type that we will be interacting with primarily, so 3:49 the domestic airline and international airline. 3:53 Note that I'm not going to implement the private airline for 3:57 this example because it's more or less identical. 3:59 So down below the control tower, let's create a new type. 4:03 We'll say struct DomesticAirline and 4:07 remember all our airlines conform to the Airline protocol. 4:11 So we'll say Airline and open up the body. 4:16 For this narrow example, 4:19 all we need to conform to Airline is to implement three things. 4:21 So we need the descent speed, which we'll make a constant. 4:24 We need the type, which is also constant. 4:30 And finally, we need a single method which is requestLandingInstructions. 4:36 Now the body of this method is all too simple, all we're going to say is return 4:42 ControlTower.land, and we'll pass in self. 4:47 So over here, we're just asking the ControlTower for landing instructions. 4:54 Whoops, we need that, there we go. 4:59 Now similarly, we could implement the international flight type, 5:01 but that's not even necessary because it's almost identical. 5:05 We can change this name and it's exactly the same. 5:09 Most of the work is being done by the Airline protocol, 5:12 so we really don't need to have anything in here. 5:15 Ignoring the fact that in a more complex example, 5:18 there would be more differences between domestic and 5:21 international flights, in our case it's nearly identical. 5:23 I could change the struct name like I just mentioned, and 5:28 we really wouldn't have to change anything else. 5:30 This indicates, if you think about it, that we may not even need these types. 5:33 We may not need DomesticAirlines and InternationalAirlines separately. 5:37 Let's take a break here. 5:42 And in the next video, we'll explore how protocols can help the situation further. 5:43
You need to sign up for Treehouse in order to download course files.Sign up