Introduction To Protocol Oriented Programming

Jimmy M Andersson
The Startup
Published in
6 min readDec 22, 2018

--

Protocol Oriented Programming has gained a lot of traction and become a buzz word in the Swift community over the last couple of years. Some love it and some hate it, but what is it really? What problems is it supposed to solve? And how does it relate to our beloved Object Oriented Programming?

What Is Protocol Oriented Programming?

Protocol Oriented Programming is not, although its name might suggest otherwise, a competitor or substitute for Object-Oriented Programming. It’s a way to think about a very specific problem set, that will help you create flexible, maintainable and easy-to-read code. It should be thought of more as a complement to the Object Oriented approach (in fact, Object Oriented Programming already incorporates a number of the central ideas in Protocol Oriented Programming).

We will start by looking into how protocols (or interfaces, as they’re called in many other languages) can help us with encapsulation and information hiding. Take a look at this example of a Car and a Carrier class:

class Car {
var position: CGPoint
var isLocked = true

init(position: CGPoint) {
self.position = position
}

public func move(x: CGFloat, y: CGFloat) {
self.position.x += x
self.position.y += y
}

public func lock() {…

--

--

Jimmy M Andersson
The Startup

Software engineer with a passion for data and machine learning