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