Protocols- Oriented programming in swift

Swift has an additional interesting aspect known as protocol. Protocols are a latest technique of viewing class hierarchy and inheritance. In a predictable object oriented programming environment, you define classes, which explain objects and the functionality they offer, along with the properties they have. Then, you subclass your classes, inheriting each of their functional ity and properties. Your subclasses can then offer additional functionality and properties, or override parts of their superclasses. These subclasses can then be subclassed further. Let’s stare at an example of a typical class hierarchy:

class Animal {

    func makeSound() {

        print("Implement me!")

    }

    

    func move() {

        print("Implement me!")

    }

}

 

class Dog: Animal {

    override func makeSound() {

        print("Woof.")

    }

    

    override func move() {

        print("walk around like a dog")

    }

    

    func bite() {

        print("bite")

    }

}

 

class Cat: Animal {

    override func makeSound() {

        print("Meow.")

    }

    

    override func move() {

        print("walk around like a cat")

    }

    

    func scratch() {

        print("scratch")

    }

}

This example demonstrates a hierarchy in which we define a base class, Animal, and then subclass it. The Dog subclass overrides the default functionality of the Animal class, and also include a bite() method, which permits dogs to bite things. The rat subclass also overrides the base class, but it includes a scratch() method, permitting cats to scratch people.

Do you observe any issues with this example? It might not seem like there is anything immediately wrong with this structure, but in reality, there are more things that could be improved. What if someone else subclasses Animal but forget to override makeSound() and / or move()? Here this will just print “Implement me!”

class Tiger: Animal {

    func eat() {

        print("Eat like a tiger")

    }

}

 
let animal: Animal = Tiger()

animal.makeSound()  

animal.move()

This following example is simple, but imagine what could happen if Animal had dozens of functions and properties that required to be overriden. What would happen?

In Objective C, these situations available us with things called abstract base classes. Abstract base classes are like Animal in the fact that they do not offer much functionality on their own. They offer a list of things that require to be overriden, without actually offering functionality on their own. A distinctive Objective C base class may toss an assertion or otherwise crash the program it’s in if it’s used directly. For this reason, abstract base classes are documented as abstract, and developers who utilize them are told to never interact with them directly, and instead interact with one of their subclasses.

In Swift, there is no abstract class. However, Swift (and Objective C) both offers a better way to approach situations such as this one: protocols. While protocols are present together Swift and Objective C, Swift protocols are much more influential. For the same issues, we can execute Animal as a protocol like this:

protocol Animal {

    func makeSound()

    func move()

}

 

struct Dog: Animal {

    func makeSound() {

        print("Woof.")

    }

    

    func move() {

        print("walk around like a dog")

    }

    

    func bite() {

        print("bite")

    }

}

 

struct Cat: Animal {

    func makeSound() {

        print("Meow.")

    }

    

    func move() {

        print("walk around like a cat")

    }

    

    func scratch() {

        print("scratch")

    }

}

Here what is different? We still have a dog and cat which both offer the same functionality that their subclassed counterparts did. But, this time, these animals both assume a familiar protocol. You notice, a class defines what an object is, but a protocol defines what an object does. You can still utilize the protocol related to what we did before:

let animal: Animal = Dog()

animal.makeSound()

animal.move()

Both Cat and Dog be conventional to the Animal protocol. And, it is compulsory for them to execute both makeSound and move methods. When assuming a protocol, the compiler enforces that any class or struct should offer the implementation of the required method as specified in the protocol declaration. Thus, it is not viable to have a struct or class like this:

swift-protocol-error

The compiler will provide you an error to ensure you offer the required implementation of the protocol.

In this tutorial, we will confer protocols more in an extension, showcasing more situations where protocols can be useful. Just remember that neither subclassing nor protocols can solve each single programming issue. Sometimes, you require both. Don’t get too hung up on one or the other, as both are extremely helpful and influential.

Last Update: September 12, 2018  

September 12, 2018   88   Nandini R    Swift Significance    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn