Loading video player…

Understanding the Point of Protocols

00:00 The very first thing you need is to make sure you understand protocols. If you understand what they’re useful for, you will have no difficulty in actually using them in your code.

00:11 So this is really the hardest lesson in all of the course. So let’s dive right into it. The first thing you need to make sure you understand is that using inheritance from object-oriented programming creates a hierarchy of classes.

00:25 You have a parent class and then some child classes, and then maybe some child classes of those first-level child classes. And so if you keep going down the inheritance tree, you are building a hierarchy where each child has access to the methods and attributes of the parent class.

00:42 And protocols are actually orthogonal to this hierarchy. The point of protocols is to define sets of behaviors that many different classes might exhibit, and that are not necessarily tied to this hierarchy.

00:58 This is all very abstract. Let’s show you an example, a concrete example. Imagine you’re building a very complex simulation of ecosystems on planet Earth, and right now you’re trying to model the animal kingdom.

01:15 And you might think, well, you’ll define a class Animal that is going to be the parent class for all species of animals that exist. And then you might think of creating child classes for mammals, and fish and birds, and others.

01:30 And you take this idea from biology because biology does this. You can classify animals as mammals, fish, birds, etc. So you think this is a good idea. And then you might think, alright, for mammals, I will define methods that allow mammals to walk and to run.

01:48 And fish will know how to dive and to swim. And birds will know how to take off and fly and land. And so you’re associating methods related to movement to each of these three classes, which will in turn have different animal species as subclasses.

02:06 And this might look like a good idea, but as soon as you start thinking about concrete animal species, everything breaks off.

02:14 For example, a whale is a mammal, so it should go under mammal in your hierarchy. However, whales cannot walk and they cannot run. And the flying fish is a fish, but it can also fly, but it doesn’t make sense to put the flying fish under birds.

02:33 And if you think about penguins and ostriches, those are birds, but those cannot fly. They can walk and they can kind of run, and penguins can dive and swim, but they definitely cannot fly.

02:44 So you can see that these movement behaviors, they’re not really necessarily tied to your hierarchy, or maybe you thought they were, but that doesn’t really work very well.

02:56 So what you need to do is you need to take a step back. You can still use this hierarchy. This is still fine. It’s just that the movements, the behaviors that you wanted to describe, they do not go directly on the hierarchy.

03:10 Because again, if you think about it, different species exhibit different behaviors. Sharks can swim and they’re a fish, that’s fine. But flying fish, they can both swim and fly.

03:23 And bats can both walk and fly. And penguins, they’re birds and they can walk and swim, but they definitely cannot fly. So these colorings on each of the species that are related to the behaviors, this is what the protocols are for.

03:40 The protocols define the behaviors that you care about. They’re not baked into the hierarchy directly because they span across the full tree. And so much so that you could even think about having planes and submarines, because planes, they can also take off and fly and land.

03:58 So they might be seen as flyers, and submarines can also dive and swim. So those could also be seen as swimmers. And this is how protocols are related to duck typing.

04:10 You might have heard, if it quacks like a duck and if it walks like a duck, it must be a duck. And this is exactly what we have here. When we think about whales and sharks and flying fish and penguins and submarines, what we say is, if it dives like a swimmer and if it swims like a swimmer, it must be a swimmer.

04:27 It doesn’t matter if it’s an animal, a mammal, fish, bird, or if it’s a submarine, something that is manmade. So this is the idea of protocols. It’s to isolate useful behaviors from your class hierarchy.

04:41 If you understand this, then you are good to go.

Become a Member to join the conversation.