What is Protocol Oriented Programming?

Barış GÖRGÜN
Orion Innovation techClub
3 min readOct 24, 2021

Protocol Oriented Programming (POP), a new programming framework that addresses issues with Object Oriented Programming (OOP) was introduced at WWDC 2015.

All programming languages have the concept of inheritance. One of the most important concepts of Object-Oriented Programming languages is the concept of Inheritance. Swift is not a language that supports Multiple Inheritance. It is a language that supports Single Inheritance.

We cann’t inherit two different classes as seen in the example

With this, object-oriented concepts don’t work well with structs and enums. For example, a struct cannot inherit from another struct, an enum cannot inherit from another enum. Therefore, inheritance, which is one of the basic concepts of object-oriented; It cannot be applied to value types such as struct, enum but enums and structs can be applied to protocols.

Example of structs inheriting protocols

Now, let’s understand why we need protocols by coding.

The penguin is a bird as a species, but it does not fly like other birds. All birds Walk and Fly as locomotion, but Penguins Swim and Walk.

First of all, we create our bird class and add the movement abilities of the birds.

If we Inheritance from the bird class while creating the Penguin, we will inherit the non-penguin flying feature. When we remove the flying feature from the bird class, we will have removed a skill that is a common feature of all other birds.

The POP(Protocol Oriented Programming) approach has emerged as a solution to such problems. Let’s define flying, walking and swimming features with the following protocols.

Let’s inherit the capabilities we need from the protocols when creating our Penguin class.

Thus, we have inherited features that belong only to Penguin.

Protocols can be used similarly with structs and enums.

Classes, structs, and enums must meet the requirements defined in the protocols. As in the example above, the Penguin class must contain the swim and walk methods. Methods defined in protocols do not have a body. Protocols leave what methods should do to the class, struct, or enum.

Defining Variable to Protocol

When we add a variable in Swift protocol, we need to write getter and setter next to it in parentheses. The keywords get and set next to the variable represent that this variable can be both read and modified. If we don’t want it to be changed, we should just add get inside the parenthesis.

Swift Protocol Extension

We can add extensions to protocols. We do not have to use the methods or variables we have added with the Extension in the classes or structs we inherit.

For example ;

When we define the run function in our Walkers class, we do not have to use this function in the penguin class.

Conclusion

POP(Protocol Oriented Programming) addresses the points where OOP(Object Oriented Programming) is insufficient for programmers, and gives the programmer a more free range of action with the protocol extension.

Thank you for reading and happy coding.

--

--