A Basic Introduction to Protocols in Swift

Jose Ortiz Costa
4 min readJan 14, 2019
A basic introduction to Protocols in Swift

Protocols, also know as interfaces in other programing languages such as Java, are a powerful component in the Swift programming language that define a blueprint of methods and properties that can be implemented by enums, structs or classes conforming those protocols . Protocol oriented programing can solve a specific set of architecture problems.

In this article, we are going to see one specific use case to solve a specific architecture problem using the Swift programing language where protocols are very useful. Take into consideration that the ideas exposed in this article are not language dependent. To put it differently, they can be used in different programing languages under their own set of rules.

Take into consideration that the target audience of this article are people who are trying to understand protocol oriented programing. Even if you are an experienced programmer, the ideas exposed in this article may be a good refresher.

After this read, you’ll understand better when and where to use the protocol oriented programing in your apps. Let’s get started.

Use Case

Imagine for a moment that you are tasked to create a program that replicates the necessary steps to delivery a package.

The set of rules for this given task are the following:

  1. The worker delivering the package can ONLY deliver it
  2. The person receiving the package can ONLY open it.

Very simple right?. However, if there is something unique that I learned in this field is that the concept of simple tasks does not exist in the programming world. Even the most simple task needs some thinking and design. Let’s see why.

The code without using protocols

The above code implements our use case without using protocol oriented programing. It is composed by three classes that define exactly what we are trying to accomplish.

However, as you can see, the class Deliverer contains a design flaw where its method openPackages() is violating one of our rules: the worker delivering the package can ONLY deliver it. Since this method takes as a parameter an object representing a package, it has free access to the open() method provided by the Package class.

There are many strategies available in order to solve this problem. However, in my opinion, the most elegant one is using protocol oriented programming to split responsabilites between objects and restrict access to the data in the class Package.

Protocol oriented programming solution

Let’s see how the code looks like using protocol oriented programing to solve this design flaw.

As you can see, in this version of the code where we used protocol oriented programming, we included two new protocols, Deliverable and Openable. Both protocols are blueprints that define the actions to take in order to deliver and open any object conforming them.

Why can they deliver and open any object and not only packages? Because we want to reuse our code as much as possible. For instance, by extending both protocols to any object, we can deliver or open cars, baggage, any type of package….etc, as long as, those classes conform those protocols.

Let’s go to the point, as you can see in the code above, the class Package implements both protocols because a package can be opened and delivered. However, the class Receiver only conforms the protocol Openable because the receiver is only interested in opening the package. Finally, the class Deliverer can only deliver a package because it conforms the Deliverable protocol. However, its openPackage() method does not work anymore because the parameter passed into the function is Deliverable but not Openable. Therefore, this protocol oriented design conforms the rules for this task.

The other way around is also true. According to our design, a receiver cannot deliver a package. However, it would make sense if a receiver could deliver a package e.g. to the store by implementing the Deliverable protocol as well. I chose the first design rather than the latest for the sake of simplifity.

Final thoughts

In this article we have learned how to solve a specific problem using protocol oriented programing. Take into consideration that protocols only solve a set of specific problems in the design of your app. However, it is important to have the knowledge to determine when they are needed, like in the specific use case provided in this article.

Where to go from here? In my next article, I am going to talk about delegation using protocols, which is an important concept in IOS development to pass data between ViewControllers.

Keep tuned!!!.

--

--

Jose Ortiz Costa

Software Engineer, Computer Science Instructor at SFSU and IOS Enthusiast