DEV Community

loading...
DSC CIET

Protocol Oriented Programming in Swift

neeraj3508 profile image Neeraj Gupta Updated on ・3 min read

Brief Introduction About Swift

Swift is a language developed by Apple which is made using modern approach, include safety features and software design patterns. As the name suggests swift is fast, safe and easy to use. It is basically made a replacement for c-based family(C, C++ and Objective-C). It can be used to make apps and can be also used for cloud services and it is among the fastest growing languages.

Protocols Oriented Programming in Swift

Defination

Protocol as the name suggests is a system of rules that must be implemented.

Why to use protocols

Let's say we want some common functionality that must be implemented to every class that conforms ( comply with rules ) to it. Let's say we have men and women, their body structure is different but they all have common functionality like eating, sleeping and walking,etc.

So, how can i tell swift that although they have diffferent structure but they are humans and have same functionality. Let's see that in code.

General Syntax

protocol name {
    statements
}
Enter fullscreen mode Exit fullscreen mode

We use keyword protocol to tell swift that we are defining protocol

Coding Example

protocol Human {
    func eating()
    func walking()
    func sleeping()
}

class Women : Human { // Here Swift will say "Type 'Women' does not conform to protocol 'Human' and fix will be to add protcol stubs(functions)"

}

class Men : Human { // Here Swift will say "Type 'Men' does not conform to protocol 'Human' and fix will be to add protcol stubs(functions)"

}
Enter fullscreen mode Exit fullscreen mode

When we will click on fix we will see something like this

protocol Human {
    func eating()
    func walking()
    func sleeping()
}

class Women : Human {
    func eating() {
        <#code#>
    }

    func walking() {
        <#code#>
    }

    func sleeping() {
        <#code#>
    }
}

class Men : Human {
    func eating() {
        <#code#>
    }

    func walking() {
        <#code#>
    }

    func sleeping() {
        <#code#>
    }
}
Enter fullscreen mode Exit fullscreen mode

Now warning will go away as we have implemented all the protocol stubs. Let's add some functionality

protocol Human {
    func eating()
    func walking()
    func sleeping()
}

class Women : Human {
    func eating() {
        print("Women is eating")
    }

    func walking() {
        print("Women is walking")
    }

    func sleeping() {
        print("Women is sleeping")
    }
}

class Men : Human {
    func eating() {
        print("Men is eating")
    }

    func walking() {
        print("Men is walking")
    }

    func sleeping() {
        print("Men is sleeping")
    }

}

// Let's make instance of both classes
let objMen = Men()
let objWomen = Women()

//Let's check if all functionalities are working
objMan.walking() //Man is walking
objMan.eating() //Man is eating
objMan.sleeping() //Man is sleeping
objWomen.eating() // Women is eating
objWomen.sleeping() //Women is sleeping
objWomen.walking() //Women is walking
Enter fullscreen mode Exit fullscreen mode

So, now let's make a general Person class which will tell some common fact about walking

protocol Human {
    func eating()
    func walking()
    func sleeping()
}

class Women : Human {
    func eating() {
        print("Women is eating")
    }

    func walking() {
        print("Women is walking")
    }

    func sleeping() {
        print("Women is sleeping")
    }
}

class Man : Human {
    func eating() {
        print("Man is eating")
    }

    func walking() {
        print("Man is walking")
    }

    func sleeping() {
        print("Man is sleeping")
    }
}

class Person {
    func burnCalories(gender: Human) {
        gender.walking() //Man is walking
        print("This person burned calories by walking") //This person burned calories by walking
    }
}

// Let's make instance of both classes
let objMan = Man()
let objWomen = Women()
let objPerson = Person()

objPerson.burnCalories(gender: objMan)

Enter fullscreen mode Exit fullscreen mode

Here in burnCalories method we said that it's parameter gender should conform to Human protocol and when we passed objMan, we know that it conforms to Human protocol. If we had passed the class that didn't conform to Human protocol then there would had been error that the passed parameter does not conform to protocol. This way we created a general method and we can pass any class or struct that conform to this protocol. if we would have defined function like below.

class Person {
    func burnCalories(gender: Man) {
        gender.walking() //Man is walking
        print("This person burned calories by walking") //This person burned calories by walking
    }
}
Enter fullscreen mode Exit fullscreen mode

In this case we are bound to pass the instance of Man class or struct. So, if we see this is not a general code and this will increase our work and will not be resusable for future or for other classes or structs and will increase our work as we won't be creating this method for every single class/struct.

I hope this gave an brief intro about protocol oriented programming. If you want to add something feel free to comment as i am myself a learner and sharing the knowledge i have with you all.

Discussion (2)

pic
Editor guide
Collapse
abhishekmansa profile image
abhishek-mansa

Awesome content!

Collapse
neeraj3508 profile image
Neeraj Gupta Author

Thanks abhishek, i am glad that you liked and appreciated this.