$30 off During Our Annual Pro Sale. View Details »

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. Protocol Oriented
    Programming
    @attheodo

    View Slide

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

    View Slide

  3. @attheodo
    “Everything we can do with classes
    we can do with structs, enums and
    protocols”

    View Slide

  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.

    View Slide

  5. @attheodo
    “Objective-C has had protocols for
    years…duh”

    View Slide

  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

    View Slide

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

    View Slide

  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.

    View Slide

  9. @attheodo
    Whatcha say?
    Complex class hierarchies often lead to
    violations of the DRY principle.
    class Dog {
    func bark() {
    print("woof")
    }
    }
    class GermanShepherd: Dog {
    }

    View Slide

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

    View Slide

  11. @attheodo
    Complex Class Hierarchies
    class Dog {

    func sniffDrugs() { … }
    }
    class GermanShepherd: Dog { … }
    class Labrador: Dog { … }
    class Schnautser: Dog { … }
    I don’t sniff drugs you !@#!#@$%

    View Slide

  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...

    View Slide

  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

    View Slide

  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!

    View Slide

  15. @attheodo
    Practical Protocol Oriented
    Programming

    View Slide

  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()
    }
    ...
    }

    View Slide

  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
    }
    }
    }

    View Slide

  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)
    ...

    View Slide

  19. @attheodo
    Remember...
    Always start with a protocol.

    View Slide

  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/

    View Slide

  21. @attheodo
    Questions?

    View Slide