This workshop will be retired on May 31, 2020.
Defining the Exercise3:56 with Pasan Premaratne
Let's take a look at the example we're going to be working on in this workshop.
[MUSIC] 0:00 Hi. 0:04 My name is Pasan. 0:05 And welcome to the Protocol Oriented Programming with Swift 2 workshop. 0:06 At this point in your Swift education, 0:10 you've learned about two important ways to model data. 0:13 One is using an inheritance-based approach with reference types or classes. 0:16 And the second is with an interface or protocol-based approach. 0:22 We also looked at simple examples of how to implement the concepts 0:26 behind these approaches, but we didn't look at anything fairly involved. 0:30 In this workshop we're going to take a small break from IOS development and 0:35 focus on Swift again, by building a model. 0:39 Unlike other courses we've done, this model will have a decent amount of 0:42 complex rules that we will have to accommodate. 0:46 In truth, most of the models we've built so far have been quite simple. 0:49 So, before we learn anything new, 0:53 let's work through an example that simply reinforces our existing knowledge. 0:55 This course will be a bit different in terms of style and 1:00 pacing but because it's less about new content and 1:03 more about working through this exercise you should be fine. 1:07 Okay so what is this example exactly? 1:10 We're going to model a control tower at a smallish airport. 1:13 At this airport let's call it the Treehouse airport, 1:18 we have a few different kinds of flights that land. 1:21 So we have domestic flights, international flights, and 1:24 a few private planes that land. 1:28 And our control tower needs to know how to handle these landings. 1:30 What do we mean by handle them? 1:35 Planes come in different sizes and fly at different speeds. 1:37 So first up, based on the approach speed into an airport, 1:41 the control tower needs to tell the pilot which runway is best suited for landing. 1:45 Our control tower needs to figure out what runway we're going to use. 1:50 Which runway is most optimal given a particular plane. 1:55 Typically an airplane can't just park at any gate in an airport. 1:59 They pay fees to have access to a certain terminal and a number of gates. 2:03 In our model, there are three terminals, each with a few different gates. 2:08 So, for example, a particular airline can only park at an open gate in terminal A. 2:12 This wouldn't be hard except that we have to account for 2:18 a few different domestic, international, and private planes. 2:21 So depending on the category, the control tower first tells a plane 2:25 which runway to use, then which terminal to land at, and then assigns an open gate. 2:29 If there are no gates available, the plane still needs to land. 2:34 So we tell the plane to land at a particular runway and 2:38 then taxi in the relevant zone, until a gate opens up. 2:41 At first look, this is somewhat simple to implement. 2:45 The tricky part is writing code that is extensible. 2:48 We want this to work not only for a limited set of airlines that we define 2:52 now, but for any situation that may arise down the road. 2:55 It should be fairly trivial, for example, down the road to add a helicopter 3:00 landing pad and allow the control tower to direct the pilot to go there as well. 3:04 Now there are two primary ways we can do this using inheritance or protocols. 3:10 We get into a lot of practice with inheritance when building iOS apps 3:15 because it is the primary pattern for object families for most of the iOS SDK. 3:19 Since we're recreating some classes of UIViewController, UIView and 3:24 other classes all the time, I wanted to focus on a protocol oriented approach for 3:28 this course. 3:33 I'm going to write the code for 3:35 this example in a single page application template in XCode. 3:36 You are free to do that or use a playground file. 3:41 There's nothing inherently iOS related in this, but playgrounds are but fickle. 3:44 And that makes for annoying screen casting. 3:49 Okay, let's get started with code in our next video. 3:52
You need to sign up for Treehouse in order to download course files.Sign up