Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CocoaHeads SKG #9 - Protocol Oriented Programming

CocoaHeads SKG #9 - Protocol Oriented Programming

by @attheodo

CocoaHeadsSKG

January 02, 2017
Tweet

More Decks by CocoaHeadsSKG

Other Decks in Technology

Transcript

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

    with structs, enums and protocols”
  2. @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.
  3. @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
  4. @attheodo In ObjectiveC the only way to share functionality between

    classes is inheritance through Polymorphism.
  5. @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.
  6. @attheodo Whatcha say? Complex class hierarchies often lead to violations

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

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

    { … } } class GermanShepherd: Dog { … } class Labrador: Dog { … } class Schnautser: Dog { … } I don’t sniff drugs you !@#!#@$%
  9. @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...
  10. @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
  11. @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!
  12. @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() } ... }
  13. @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 } } }
  14. @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) ...
  15. @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/