Protocol-Oriented Programming in iOS: the Correct Way to Use Swift by JC Velasquez

19
Protocol-Oriented Programming The *correct* way to use Swift

Transcript of Protocol-Oriented Programming in iOS: the Correct Way to Use Swift by JC Velasquez

Protocol-Oriented Programming

The *correct* way to use Swift

What’s Protocol-Oriented?

Protocols in Swift

• Mostly the same with @protocols in Objective-C

• Protocols in Swift can be extended like a class extension in Obj-C (protocol extension)

• Protocol extensions can have implementation code

• Made more powerful by generics in Swift

Advantages

• You solve problems related to inheritance

• You get rid of implicit sharing

• You end up with more readable code

Isn’t inheritance a good thing?

Composition vs Inheritance Principle

class Dog { func bark() { print("Bark!") }}

class GermanShephard: Dog {}

let myDog = GermanShephard()myDog.bark() // prints "Bark!"

Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/

Composition vs Inheritance Principle

class Dog { func bark() { print("Bark!") }}

class GermanShephard: Dog { func sniffDrugs() { if drugs { bark() } }}

class BelgianMalinois: Dog { func sniffDrugs() { if drugs { bark() } }}

class Poodle: Dog {}

Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/

Composition vs Inheritance Principle

class Dog { func bark() { print("Bark!") }}

class DrugSniffingDog: Dog { func sniffDrugs() { if drugs { bark() } }}

class GermanShephard: DrugSniffingDog {}

class BelgianMalinois: DrugSniffingDog {}

class Poodle: Dog {}

Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/

Composition vs Inheritance Principle

class Dog { func bark() { print("Bark!") }}

class DrugSniffingDog: Dog { func sniffDrugs() { if drugs { bark() } }}

class GermanShephard: DrugSniffingDog {}

class BelgianMalinois: DrugSniffingDog {}

class Poodle: Dog {}

Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/

Composition vs Inheritance Principle

protocol Barker { func bark()}

protocol Swimmer { func swim()}

protocol DrugSniffer { func sniffDrugs()}

extension Barker { func bark() { print("Bark!") }}

extension Swimmer { func swim() { print("Splash!") }}

extension DrugSniffer { func sniffDrugs() { print("I found drugs!"); }}

class GermanShephard: Barker, DrugSniffer {}

class BelgianMalinois: Barker, Swimmer, DrugSniffer {}

class Poodle: Barker, Swimmer {}

let myDog = BelgianMalinois()myDog.bark() // prints "Bark!"myDog.swim() // prints "Splash!"myDog.sniffDrugs() // prints "I found drugs!"

Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/

Isn’t sharing a good thing?

Bad sharing

class Temperature {

var celsius: Double = 0

var fahrenheit: Double {

get { return celsius * 9 / 5 + 32 }

set { celsius = (newValue - 32) * 5 / 9 }

}

}

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

Bad sharing

let home = House()

let temp = Temperature()

temp.fahrenheit = 75

home.thermostat.temperature = temp

temp.fahrenheit = 425

home.oven.temperature = temp

home.oven.bake()

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

Bad sharing

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

House

Oven

Thermostat

Temperature

Bad sharing

struct Temperature: Equatable {

var celsius: Double = 0

var fahrenheit: Double {

get { return celsius * 9 / 5 + 32 }

set { celsius = (newValue - 32) * 5 / 9 }

}

}

func ==(lhs: Temperature, rhs: Temperature) -> Bool {

return lhs.celsius == rhs.celsius

}

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

Bad sharing

let home = House()

var temp = Temperature()

temp.fahrenheit = 75

home.thermostat.temperature = temp

temp.fahrenheit = 425

home.oven.temperature = temp

home.oven.bake()

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

Bad sharing

Taken from https://developer.apple.com/videos/play/wwdc2015/414/

House

Oven

Thermostat

Temperature

Temperature

–Heraclitus (Panta Rhei “Everything Flows”)

“Ever-newer waters flow on those who step into the same rivers.”