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.”
jobs@smartwave.ph

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

  • 1.
  • 2.
  • 3.
    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
  • 4.
    Advantages • You solveproblems related to inheritance • You get rid of implicit sharing • You end up with more readable code
  • 5.
  • 6.
    Composition vs InheritancePrinciple 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/
  • 7.
    Composition vs InheritancePrinciple 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/
  • 8.
    Composition vs InheritancePrinciple 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/
  • 9.
    Composition vs InheritancePrinciple 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/
  • 10.
    Composition vs InheritancePrinciple 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/
  • 11.
    Isn’t sharing agood thing?
  • 12.
    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/
  • 13.
    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/
  • 14.
    Bad sharing Taken fromhttps://developer.apple.com/videos/play/wwdc2015/414/ House Oven Thermostat Temperature
  • 15.
    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/
  • 16.
    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/
  • 17.
    Bad sharing Taken fromhttps://developer.apple.com/videos/play/wwdc2015/414/ House Oven Thermostat Temperature Temperature
  • 18.
    –Heraclitus (Panta Rhei“Everything Flows”) “Ever-newer waters flow on those who step into the same rivers.”
  • 19.