1 00:00:00,630 --> 00:00:04,200 You might be confused as to why we're starting on the control tower 2 00:00:04,200 --> 00:00:07,080 without creating any concrete types first. 3 00:00:07,080 --> 00:00:09,960 The goal of this exercise is to allow any aircraft, 4 00:00:09,960 --> 00:00:12,210 whether it is domestic, international, 5 00:00:12,210 --> 00:00:16,850 or private to request landing instructions from the control tower. 6 00:00:16,850 --> 00:00:19,600 These are entirely different types altogether. 7 00:00:19,600 --> 00:00:23,060 But we've learned in the past that we can use a higher type 8 00:00:23,060 --> 00:00:25,760 to group different subtypes together. 9 00:00:25,760 --> 00:00:31,050 A higher type can be a super class of different base classes or, 10 00:00:31,050 --> 00:00:34,610 as in our case, it can be a protocol type. 11 00:00:34,610 --> 00:00:38,530 All airlines will conform to the airline protocol. 12 00:00:38,530 --> 00:00:42,120 So we can use this protocol as the higher type 13 00:00:42,120 --> 00:00:45,890 with which we implement control tower functionality. 14 00:00:45,890 --> 00:00:49,740 As always, this will become more clear as we write code. 15 00:00:49,740 --> 00:00:52,865 So below landing instructions let's add a new comment marker. 16 00:00:52,865 --> 00:00:58,380 We'll say MARK: Control Tower. 17 00:00:58,380 --> 00:01:01,030 The object that represents the control tower 18 00:01:01,030 --> 00:01:03,750 should know about a few different things. 19 00:01:03,750 --> 00:01:06,700 It needs to know what runways the airport has, 20 00:01:06,700 --> 00:01:11,730 the different terminals available, and the status of gates at each terminal. 21 00:01:11,730 --> 00:01:15,440 Because all of these change at different points in time, that is, 22 00:01:15,440 --> 00:01:20,140 they have state, we're going to use a class to represent this object. 23 00:01:20,140 --> 00:01:23,660 So we'll say class ControlTower. 24 00:01:26,280 --> 00:01:30,840 Now this class is going to contain methods that define the logic of how 25 00:01:30,840 --> 00:01:32,830 an airline lands. 26 00:01:32,830 --> 00:01:35,840 We don't want anyone else in this code base 27 00:01:35,840 --> 00:01:40,160 to be able to subclass ControlTower and modify those things. 28 00:01:40,160 --> 00:01:43,870 So, we're going to add a keyword, final. 29 00:01:43,870 --> 00:01:48,450 And we've learned before that this prevents anything in this class from being 30 00:01:48,450 --> 00:01:50,780 overridden or sub-classed. 31 00:01:50,780 --> 00:01:54,090 Okay, the first thing we want to tackle is the runway. 32 00:01:55,120 --> 00:01:58,510 An airport always has a fixed set of runways, so 33 00:01:58,510 --> 00:02:01,610 an enum is a good object to model this. 34 00:02:01,610 --> 00:02:05,570 We've briefly talked about nested types in the past, how types and 35 00:02:05,570 --> 00:02:09,370 swift can define any amount of nested types. 36 00:02:09,370 --> 00:02:13,650 By nested, I mean a new type inside an existing type. 37 00:02:13,650 --> 00:02:18,150 Since only a control tower needs to know about the entire set of runways at 38 00:02:18,150 --> 00:02:23,090 an airport, we'll define the runway type as a nested type inside control tower. 39 00:02:24,110 --> 00:02:28,100 So in here let's create a new runway type as an enum. 40 00:02:31,300 --> 00:02:32,800 So we'll give it a couple cases. 41 00:02:32,800 --> 00:02:36,733 We'll say, case R22L, 42 00:02:36,733 --> 00:02:41,179 of course I just made these up, 43 00:02:41,179 --> 00:02:45,800 L31R, M52J, and B19E. 44 00:02:49,041 --> 00:02:52,679 As far as I understand with my limited knowledge of flying, 45 00:02:52,679 --> 00:02:57,950 the runway an airplane lands on depends on its approach or descent speed. 46 00:02:57,950 --> 00:03:01,430 Basically if you're coming in faster you need longer runways. 47 00:03:01,430 --> 00:03:06,220 So in this type, let's create a static method that returns a runway 48 00:03:06,220 --> 00:03:09,390 based on the speed of the approaching aircraft. 49 00:03:09,390 --> 00:03:15,780 Remember to create a static method or a type method, we say static, func, runway, 50 00:03:17,450 --> 00:03:23,570 and we'll accept the speed in knots, and we'll return in runway value. 51 00:03:25,430 --> 00:03:28,900 Inside this method we'll simply switch on the speed. 52 00:03:28,900 --> 00:03:32,980 And for ranges of incoming speeds we return different values. 53 00:03:32,980 --> 00:03:39,880 So it will switch on speed and let's assume for our example that 54 00:03:39,880 --> 00:03:45,240 this first runway is defined as the shortest and the last one is the longest. 55 00:03:45,240 --> 00:03:49,530 So we can say for small a single engine aircraft, 56 00:03:49,530 --> 00:03:53,900 something like private planes, they come in under 91 knots. 57 00:03:53,900 --> 00:04:01,820 So for the first range we'll say case 0 <91. 58 00:04:01,820 --> 00:04:04,190 We're going to return R22L. 59 00:04:05,720 --> 00:04:12,400 The second range is from 91 to 120 knots, and this covers small multiengine planes. 60 00:04:12,400 --> 00:04:19,270 So we'll say case 91 to 120. 61 00:04:19,270 --> 00:04:23,811 These are going to land on runway L31R. 62 00:04:23,811 --> 00:04:30,430 Airline jets come in at 121 to 140 knots. 63 00:04:30,430 --> 00:04:34,070 That means another dot. 64 00:04:34,070 --> 00:04:40,050 And these are going to land at M52J. 65 00:04:40,050 --> 00:04:42,290 For our last category, large jets. 66 00:04:42,290 --> 00:04:46,020 So let's say long international flights, long haul flights. 67 00:04:46,020 --> 00:04:49,396 These come in at 141 to 165 knots. 68 00:04:54,176 --> 00:04:59,240 And they're going to use the longest runway, B19E. 69 00:05:01,320 --> 00:05:02,850 Now for a default case. 70 00:05:02,850 --> 00:05:07,170 So anything that comes in a higher than 165 knots we'll just say 71 00:05:07,170 --> 00:05:08,140 use the longest runway. 72 00:05:10,290 --> 00:05:13,640 Okay, now that we can figure out which runway to land on, 73 00:05:13,640 --> 00:05:17,390 let's create the main point of interaction for the control tower. 74 00:05:17,390 --> 00:05:18,590 That is the land method. 75 00:05:20,650 --> 00:05:25,660 So over here we'll say func land, and 76 00:05:25,660 --> 00:05:30,640 the land method takes an argument named airline of type Airline. 77 00:05:32,040 --> 00:05:36,020 This is the higher protocol type rather than any specific concrete type. 78 00:05:37,420 --> 00:05:40,330 And it's going to return a set of landing instructions. 79 00:05:42,590 --> 00:05:48,000 Because the airline protocol defines a descent speed property requirement, 80 00:05:48,000 --> 00:05:51,310 we can guarantee that any type that's passed in any 81 00:05:51,310 --> 00:05:56,160 type that conforms to airline will have that descent speed property. 82 00:05:56,160 --> 00:05:59,070 So we can use that information encoded in the protocol 83 00:05:59,070 --> 00:06:00,820 to figure out air runway to land on. 84 00:06:00,820 --> 00:06:09,300 So we'll say let runway = Runway.runway, which is our static method. 85 00:06:09,300 --> 00:06:15,190 And we'll say (airline.descentSpeed). 86 00:06:15,190 --> 00:06:18,260 Now, we have an error because we need to return a valid 87 00:06:18,260 --> 00:06:20,700 instance of landing instructions. 88 00:06:20,700 --> 00:06:23,030 For now, scroll back up to the struct and 89 00:06:23,030 --> 00:06:25,980 we'll add a single property to store the runway. 90 00:06:25,980 --> 00:06:27,550 So we'll say let runway. 91 00:06:28,670 --> 00:06:32,360 Now for the type of this property we can specify the control towers 92 00:06:32,360 --> 00:06:34,170 nested runway type. 93 00:06:34,170 --> 00:06:37,899 Like this, we'll say ControlTower.Runway. 94 00:06:37,899 --> 00:06:40,130 And now, we can go back down here, 95 00:06:40,130 --> 00:06:43,910 and return to Runway that we just figured out we're going to land at. 96 00:06:43,910 --> 00:06:49,630 So we'll say return LandingInstructions runway. 97 00:06:51,930 --> 00:06:57,070 With just a few lines of code, we've illustrated the flexibility of protocols. 98 00:06:57,070 --> 00:07:00,830 We haven't had any need for a concrete type just yet. 99 00:07:00,830 --> 00:07:03,760 Since the protocol guarantees the information we need, 100 00:07:03,760 --> 00:07:07,550 the descent speed will be available, we can use the property 101 00:07:07,550 --> 00:07:12,990 on the protocol type as a higher type to execute our logic. 102 00:07:12,990 --> 00:07:16,760 Okay, the next thing we need to tackle is the terminal and gate number. 103 00:07:16,760 --> 00:07:17,680 On to the next video.