This workshop will be retired on May 31, 2020.
Nested Types7:18 with Pasan Premaratne
Most of the logic will be encapsulated inside the Control Tower object. In this video, let's use a nested type to build the runway selection logic.
You might be confused as to why we're starting on the control tower 0:00 without creating any concrete types first. 0:04 The goal of this exercise is to allow any aircraft, 0:07 whether it is domestic, international, 0:09 or private to request landing instructions from the control tower. 0:12 These are entirely different types altogether. 0:16 But we've learned in the past that we can use a higher type 0:19 to group different subtypes together. 0:23 A higher type can be a super class of different base classes or, 0:25 as in our case, it can be a protocol type. 0:31 All airlines will conform to the airline protocol. 0:34 So we can use this protocol as the higher type 0:38 with which we implement control tower functionality. 0:42 As always, this will become more clear as we write code. 0:45 So below landing instructions let's add a new comment marker. 0:49 We'll say MARK: Control Tower. 0:52 The object that represents the control tower 0:58 should know about a few different things. 1:01 It needs to know what runways the airport has, 1:03 the different terminals available, and the status of gates at each terminal. 1:06 Because all of these change at different points in time, that is, 1:11 they have state, we're going to use a class to represent this object. 1:15 So we'll say class ControlTower. 1:20 Now this class is going to contain methods that define the logic of how 1:26 an airline lands. 1:30 We don't want anyone else in this code base 1:32 to be able to subclass ControlTower and modify those things. 1:35 So, we're going to add a keyword, final. 1:40 And we've learned before that this prevents anything in this class from being 1:43 overridden or sub-classed. 1:48 Okay, the first thing we want to tackle is the runway. 1:50 An airport always has a fixed set of runways, so 1:55 an enum is a good object to model this. 1:58 We've briefly talked about nested types in the past, how types and 2:01 swift can define any amount of nested types. 2:05 By nested, I mean a new type inside an existing type. 2:09 Since only a control tower needs to know about the entire set of runways at 2:13 an airport, we'll define the runway type as a nested type inside control tower. 2:18 So in here let's create a new runway type as an enum. 2:24 So we'll give it a couple cases. 2:31 We'll say, case R22L, 2:32 of course I just made these up, 2:36 L31R, M52J, and B19E. 2:41 As far as I understand with my limited knowledge of flying, 2:49 the runway an airplane lands on depends on its approach or descent speed. 2:52 Basically if you're coming in faster you need longer runways. 2:57 So in this type, let's create a static method that returns a runway 3:01 based on the speed of the approaching aircraft. 3:06 Remember to create a static method or a type method, we say static, func, runway, 3:09 and we'll accept the speed in knots, and we'll return in runway value. 3:17 Inside this method we'll simply switch on the speed. 3:25 And for ranges of incoming speeds we return different values. 3:28 So it will switch on speed and let's assume for our example that 3:32 this first runway is defined as the shortest and the last one is the longest. 3:39 So we can say for small a single engine aircraft, 3:45 something like private planes, they come in under 91 knots. 3:49 So for the first range we'll say case 0 <91. 3:53 We're going to return R22L. 4:01 The second range is from 91 to 120 knots, and this covers small multiengine planes. 4:05 So we'll say case 91 to 120. 4:12 These are going to land on runway L31R. 4:19 Airline jets come in at 121 to 140 knots. 4:23 That means another dot. 4:30 And these are going to land at M52J. 4:34 For our last category, large jets. 4:40 So let's say long international flights, long haul flights. 4:42 These come in at 141 to 165 knots. 4:46 And they're going to use the longest runway, B19E. 4:54 Now for a default case. 5:01 So anything that comes in a higher than 165 knots we'll just say 5:02 use the longest runway. 5:07 Okay, now that we can figure out which runway to land on, 5:10 let's create the main point of interaction for the control tower. 5:13 That is the land method. 5:17 So over here we'll say func land, and 5:20 the land method takes an argument named airline of type Airline. 5:25 This is the higher protocol type rather than any specific concrete type. 5:32 And it's going to return a set of landing instructions. 5:37 Because the airline protocol defines a descent speed property requirement, 5:42 we can guarantee that any type that's passed in any 5:48 type that conforms to airline will have that descent speed property. 5:51 So we can use that information encoded in the protocol 5:56 to figure out air runway to land on. 5:59 So we'll say let runway = Runway.runway, which is our static method. 6:00 And we'll say (airline.descentSpeed). 6:09 Now, we have an error because we need to return a valid 6:15 instance of landing instructions. 6:18 For now, scroll back up to the struct and 6:20 we'll add a single property to store the runway. 6:23 So we'll say let runway. 6:25 Now for the type of this property we can specify the control towers 6:28 nested runway type. 6:32 Like this, we'll say ControlTower.Runway. 6:34 And now, we can go back down here, 6:37 and return to Runway that we just figured out we're going to land at. 6:40 So we'll say return LandingInstructions runway. 6:43 With just a few lines of code, we've illustrated the flexibility of protocols. 6:51 We haven't had any need for a concrete type just yet. 6:57 Since the protocol guarantees the information we need, 7:00 the descent speed will be available, we can use the property 7:03 on the protocol type as a higher type to execute our logic. 7:07 Okay, the next thing we need to tackle is the terminal and gate number. 7:12 On to the next video. 7:16
You need to sign up for Treehouse in order to download course files.Sign up