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

Polymorphism with Protocols

Polymorphism with Protocols

Good API design is essential for writing code that scales. In this talk, we look at some characteristics of good design and how to refactor code using the protocol features in Swift.

Source code available at https://github.com/matthewcheok/polymorphism-with-protocols

Video at https://youtu.be/2fO-B4qL8ZA

Avatar for Matthew Cheok

Matthew Cheok

October 19, 2017

Other Decks in Programming

Transcript

  1. Naive Approach class Node { var value: Double? var operation:

    String? var leftChild: Node? var rightChild: Node? func evaluate() -> Double }
  2. Values class Node { var value: Double? var operation: String?

    var leftChild: Node? var rightChild: Node? func evaluate() -> Double }
  3. Operations class Node { var value: Double? var operation: String?

    var leftChild: Node? var rightChild: Node? func evaluate() -> Double }
  4. Optionals? Everywhere class Node { var value: Double? var operation:

    String? var leftChild: Node? var rightChild: Node? func evaluate() -> Double }
  5. Which can be nil? class Node { var value: Double?

    var operation: String? var leftChild: Node? var rightChild: Node? func evaluate() -> Double }
  6. Structs or Classes? class Node { var value: Double? ...

    } struct Node { var value: Double? ... }
  7. What we did • Naive class implementation • Introducing abstract

    classes • Moving into protocols (value types) • Implementing a third-party protocol (rendering) (protocol extensions)
  8. Additional Readings Inheritance, Polymorphism, & Testing Misko Hevery Everyone is

    an API designer John Sundell Playground Quicklook for Binary Trees Swift Talk