CocoaHeads SKG #9 - Protocol Oriented Programming

CocoaHeads SKG #9 - Protocol Oriented Programming

by @attheodo

5a3cfb263fe4968b76c4c0bde721f047?s=128

CocoaHeadsSKG

January 02, 2017
Tweet

Transcript

  1. Protocol Oriented Programming @attheodo

  2. @attheodo “Swift is the world’s first protocol-oriented language”

  3. @attheodo “Everything we can do with classes we can do

    with structs, enums and protocols”
  4. @attheodo In fact... $ grep -e "^struct" stdlib.swift | wc

    -l 87 $ grep -e "^enum" stdlib.swift | wc -l 8 $ grep -e "^class" stdlib.swift | wc -l 4 Swift Core is based in 95 value types and 4 reference types.
  5. @attheodo “Objective-C has had protocols for years…duh”

  6. @attheodo Indeed, BUT... • Actually they are what other languages

    call “interfaces” • Your classes still have to inherit from a superclass (NSObject) • … and they have to implement the protocol methods themselves
  7. @attheodo In ObjectiveC the only way to share functionality between

    classes is inheritance through Polymorphism.
  8. @attheodo In ObjectiveC the only way to share functionality between

    classes is inheritance through Polymorphism. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.
  9. @attheodo Whatcha say? Complex class hierarchies often lead to violations

    of the DRY principle. class Dog { func bark() { print("woof") } } class GermanShepherd: Dog { }
  10. @attheodo Complex Class Hierarchies class Dog { … } class

    GermanShepherd: Dog { func sniffDrugs() {...} } class Labrador: Dog { func sniffDrugs() { … } } class Schnautser: Dog { } code duplication
  11. @attheodo Complex Class Hierarchies class Dog { … func sniffDrugs()

    { … } } class GermanShepherd: Dog { … } class Labrador: Dog { … } class Schnautser: Dog { … } I don’t sniff drugs you !@#!#@$%
  12. @attheodo Complex Class Hierarchies class Dog { … } class

    DrugsSniffingDog: Dog { func sniffDrugs() { … } } class GermanShepherd: DrugsSniffingDog { … } class Labrador: DrugsSniffingDog { … } class Schnautser: Dog { … } It’s bearable. Until we need to represent dogs who can/can’t swim...
  13. @attheodo Enter Protocols • A protocol defines a blueprint of

    methods, properties and other requirements that suit a particular task/functionality • It can be adopted by a class, structure or enumeration to implement the requirements. • A protocol can be extended to provide default implementation to some of the requirements that conforming types can take advantage of. • Protocols are Types; Can be parameter types or return types, can be used as a type for parameters/constants, can be types in collections etc. • Protocols can inherit one or more other protocols
  14. @attheodo Enter Protocols protocol Barker { func bark() } extension

    Barker { func bark() { print(“woof”) } } protocol Swimmer { func swim() } extension Swimmer { func swim() { print(“plof plof”) } } class GermanShepherd: Barker { } class Labrador: Barker, Swimmer { } class Schnautser { } Composition vs Inheritance!
  15. @attheodo Practical Protocol Oriented Programming

  16. @attheodo Practical POP protocol ErrorAlertViewRenderer { func presentError() } extension

    ErrorPopoverRenderer where Self: UIViewController { func presentError() { let alert = UIAlertViewController(...) } } class MyViewController: UIViewController, ErrorAlertViewRenderer { … guard let error == nil else { presentError() } ... }
  17. @attheodo Practical POP protocol Fadeable {} extension Fadeable where Self:

    UIView { func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0) { self.alpha = 0.0 UIView.animateWithDuration(...) { self.alpha = 1.0 } } func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0) { Self.alpha = 1.0 UIView.animateWithDuration(...) { Self.alpha = 0.0 } } }
  18. @attheodo Practical POP protocol ReusableView: class {} extension ReusableView where

    Self: UIView { static var reuseIdentifier: String { return String(self) } } extension UITableViewCell: ReusableView {} … let cell = dequeuReusableCellWithIdentifier(MyCell.reuseIdentifier) ...
  19. @attheodo Remember... Always start with a protocol.

  20. @attheodo Where to go from here... • https://developer.apple.com/library/content/documentation/Swi ft/Conceptual/Swift_Programming_Language/Protocols.html •

    https://developer.apple.com/videos/play/wwdc2015/408/ • https://realm.io/news/appbuilders-natasha-muraschev-practical- protocol-oriented-programming/
  21. @attheodo Questions?