Get an overview of the Protocol-Oriented Programming paradigm.
- [Narrator] What is protocol oriented programming or POP. At WWDC 2015, Apple introduced Swift 2.0. And with it, a new programming paradigm called Protocol Oriented Programing. We all know wat an object is. So what's a protocol. In short, protocols serve as blueprints. A protocol describes what a type shall implement without actually defining the behavior. Here in the top, we have a CustomView class that inherits from UIView. It provides a custom pulse method that allows callers to animate the view.
Now with protocols, we cannot provide the implementation. On the bottom, the anima-table protocol only defines the pulse method requirement. Classes that adopt the protocol must implement the method. This is the fundamental difference between a protocol and a concrete type. Protocols are blueprints rather than fully functional types. When we design a software system we try to identify the elements needed to satisfy the requirements of the given system. Then, we model the relationship between these elements.
We can start with super classes and model the relationships using inheritance or we can start with protocols and model the relationships as protocol implementations. Apple tells us, "Don't start with a class, start with a protocol." In the protocol oriented approach: we start modeling our system with a protocol, rely on new concepts like protocol extensions, protocol inheritance, and protocol composition, in Swift, value semantics are preferred over reference types, that is, considering using structs, enums, and tuples rather than working solely with classes.
Why are protocols and protocol inheritance preferred over base classes and sub-classing? Because, as we will see in a moment, protocols serve as better abstractions than classes, modeling an abstraction using classes implies inheritance, inheritance has been around for a while and we've been using it to build various software products, inheritance works great until we hit some of its restrictions. Let's take a look both the benefits and the drawbacks of this fundamental object-oriented concept called inheritance.
The superclass provides the core functionality. It includes all the logic required to satisfy the requirements for that given type. If we need more specific behavior we can add subclasses. A subclass can completely override the behavior defined in the superclass or it can change it or use it just as defined in the superclass. Inheritance gives us a lot of flexibility and freedom, yet, it also has its limitations. Swift, as many other modern programming languages, prohibits multiple inheritance.
So if our subclass needs functionality from a different superclass we just can't make it work, we need to add that functionality to our superclass. We keep adding new functionality and assigning new responsibilities to the base class. Eventually, our superclass become bloated, besides, inheritance doesn't work for value types and value types our first class citizens in Swift. So there must be a better way, protocols solve all the mentioned issues.
So let's talk about protocols. Protocols have various benefits. We're not restricted to classes since any type, including value types, can implement a protocol. Although multiple inheritance is restricted for classes, a type can implement multiple protocols. We can define granular designs by creating as many protocols as needed. We define the property and method requirement without creating huge bloated super classes.
Released
1/2/2018- Comparing object-oriented programming with protocol-oriented programming
- Methods and class-bound protocols
- Adopting a protocol
- Generics
- Declaring asynchronous behavior
- Preparing and implementing fallback logic
- Implementing an app using protocol-oriented programming
Share this video
Embed this video
Video: What's Protocol-Oriented Programming?