This workshop will be retired on May 31, 2020.
Composing the Airline3:48 with Pasan Premaratne
To build our main Airline interface we combine a few different protocols to compose a specific set of requirements
With the flying protocol out of the way, let's focus on landing. 0:00 Remember the goal of this exercise is to allow any aircraft to request landing. 0:04 The control tower that we model should then deliver a set 0:09 of landing instructions to that aircraft. 0:12 So let's create another protocol. 0:15 Blank flying will keep this protocol simple and give a single requirement. 0:20 Any object that conforms to Landing needs to implement 0:25 the requestLandingInstructions method. 0:29 So we'll say func requestLandingInstructions. 0:31 And this returns a type of LandingInstructions. 0:38 For now, to make this work, let's declare this as an empty struct. 0:45 We'll add to that in a bit. 0:49 So we'll say struct LandingInstructions. 0:50 And we'll just keep it empty. 0:58 Okay, now, we have two protocols that identify the capabilities of an aircraft. 1:00 By conforming to these two, we really have most of the information we need to request 1:06 a landing in our trivial example and 1:11 figure out which runway is most appropriate. 1:13 The control tower still doesn't know which terminal and 1:17 gate to assign because the protocols don't provide any identifying information 1:20 about which airline is requesting a landing. 1:24 So let's define a third protocol. 1:27 I'm going to add it right after Landing. 1:30 We'll call this protocol AirLine. 1:32 An Airline is going to combine the Flying and Landing protocols. 1:36 That is, it's going to inherit from those. 1:40 And add a few more requirements. 1:42 Protocols in Swift, if you remember, can inherit from multiple protocols. 1:44 So we can specify that airline inherits from both Flying and Landing like this. 1:48 Airline is the protocol that our different airline types will conform to. 1:58 So we need to add the remaining information that we need for 2:03 the control tower to issue the complete set of instructions. 2:06 Right now, we have a requirement of descent speed. 2:10 Which will help us figure out which runway we need. 2:13 We have a method that allows the aircraft to request LandingInstructions and 2:15 now we need to encode the information that allows the control tower to figure out 2:20 which airline is requesting it. 2:24 Any type conforming to airline needs to indicate what kind of airline it is. 2:27 So we'll say vary type 2:33 AirlineType and just a get. 2:37 An airline type is indicated by the type, AirlineType, which, 2:42 if that didn't sound confusing enough, is going to be another protocol. 2:46 So we'll say protocol AirlineType. 2:50 And this is going to be an empty protocol. 2:56 I'll explain the reasoning for this shortly. 2:59 Note that if you're doing this in a playground, you might have to 3:02 declare this above Airline so that it all works, otherwise you'll get an unknown 3:05 type error, and that's all the Airline protocol will contain. 3:10 Again, there are many more attributes that could go in there, flight number or 3:14 route off the top of my head, and if you want to extend the model, go for it. 3:19 We now have all the protocols we need to model a particular flight and 3:23 provide information to the control tower to issue landing directions. 3:27 I know it doesn't seem like we do, but you'll see in a second. 3:31 Let's add a comment up at the top. 3:35 So after the typealias we'll say, MARK: Protocols. 3:37 In the next video, let's get started on the control tower. 3:44
You need to sign up for Treehouse in order to download course files.Sign up