From the course: Swift 5: Protocol-Oriented Programming

What is protocol-oriented programming?

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

What is protocol-oriented programming?

- [Instructor] In June 2015, Apple introduced a new programming paradigm called protocol oriented programming. The concept isn't that new at all. Protocols have been around for decades. Many object oriented programming languages rely on them as a means to define communication contracts between objects. Java, C#, or PHP use the term interface instead of protocol. In C++ we've got the abstract class. The creators of Objective-C and Swift decided to use the term protocol, so protocol is just another name for interface. But what exactly is a protocol? The protocol describes the method and property requirements that need to be implemented by conforming types. Although it is possible to implement default behavior in a protocol, we can't instantiate it. Protocols serve as blueprints, rather than fully functional types. Now, let's talk about protocol orientation. When we design a software system we identify the main entities first. Then, we model the relationships between these entities. At this stage, we can start thinking in base classes and inheritance. Alternatively, we can rely on protocols and protocol implementations. Swift provides full support for both approaches. However, Apple engineers prefer the protocol-based approach and they practice what they preach. The Swift standout library has been designed and developed using POP techniques. How does it work? "Don't start with a class, start with a protocol." In the protocol oriented approach we start modeling our system with a protocol. Swift provides built in support for the protocol oriented paradigm. We've got advanced features like protocol extensions, protocol inheritance, and protocol composition. Also, we should favor value semantics over reference types. That is consider using struts, (mumbles) and Tapas instead of working exclusively with classes.

Contents