This is the first part of a multiple part series of Protocol Oriented Programming with Swift (Actually Only with Swift, if you know any other programming language which supports Protocol Oriented Programming, please be kind enough to comment below). Let's first try to understand what the heck is a Protocol in Swift/iOS App development context. May be we should start from somewhere most of us are familiar with, at least the people who are familiar with Java (if not, you are missing a lot), yes the most popular programming language in the world.
When you think Java, you think in Object Oriented Programming and when you think OOP, you think in Interfaces ! Its not that C++ doesn't have Interfaces, but I chose Java interfaces as it closely resembles with Swift Protocols. Java Interface is an agreement or a Contract between two parties or modules of how the behavior of a class is defined through its implementation of methods. In simple terms, the Interface forces the classes implementing it to implement the methods declared inside the Interface. In this way, Protocol and Interface are like identical twins as they both forces their implementer to implement the methods (Functions in Swift) declared inside the Protocol or the Interface.
public interface DrawShape { // Let's just pick two simple shapes to draw.
. int area = 100; public void drawRect(int height, int width); public void drawSquare(int length);
}
public class ShapeDrawBuddy implements DrawShape {
. int area = DrawShape.area * 2; @Override
public void drawRect(int height, int width) { // your own way to draw a rect }
. @Override public void drawSquare(int length) { // your own way to draw a square }}
When it comes to the properties or the attributes defined inside a Java Interface, they are just constants and the implementer cannot alter or mutate the value.
However, if we are talking about Swift Protocols, the properties and variables can be mutated and altered in the implementer level. Now, that is a main difference from the surface. Let's take a look at the same code segment in Swift protocols,
public protocol DrawShape {
var area: Int {
get set
}
func drawRect(height: Int, width: Int)
func drawSquare(length: Int)
}
And another cool (Kool I meant) thing about Xcode and Swift is that, you can declare both protocol and the implementer class in the same Swift file, unlike Java where the compiler screams at you for not using separate .java files for two classes.
class ShapeDrawBuddy: DrawShape {
var area: Int = 200
func drawRect(height: Int, width: Int) {
}
func drawSquare(length: Int) {
}
}
As you can see, the implementer has the liberty to get or set the variables declared in the Protocol in Swift. If you want to make it a constant, the only two changes you need to do is,
To DrawShape
var area: Int {
get
}
and at the implementation level,
let area: Int = 200
But remember, most important thing is that a Protocol NEVER dictates how you want to implement the properties like a true interface (which Java interface isn't). The Swift Protocol just want to make sure you have implemented the variables and the methods it wants you to implement but how it is done, is up to the implementer and not to the Protocol. I think this gives more flexibility and overall autonomy at every level. I will discuss this more in the coming parts of the same series.
Please remember that I may not publish the second part of this in the immediate next blog post as I believe in switching between different topics for each day is important. Thanks and see you in the next post. Until that, Happy Coding !
Add new comment