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

Protocol and Value
 Oriented Programming

Buzzvil
November 07, 2018

Protocol and Value
 Oriented Programming

By Jerry

Buzzvil

November 07, 2018
Tweet

More Decks by Buzzvil

Other Decks in Programming

Transcript

  1. A protocol defines a blueprint of requirements that suit a

    particular task or piece of functionality. The protocol can then be adopted by a class to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements.
  2. A protocol defines a blueprint of methods, properties, and other

    requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements. In addition, you can extend a protocol to implement some of these requirements.
  3. A protocol defines a blueprint of methods, properties, and other

    requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements. In addition, you can extend a protocol to implement some of these requirements.
  4. Property/method requirements for class/struct/enum protocol FullyNamed { var fullName: String

    { get } func printFullName() } class Cat: FullyNamed { var fullName: String func printFullName() { print(“A cat named \(fullName)") } } struct Person: FullyNamed { var fullName: String func printFullName() { print(“A person named \(fullName)") } } protocol Togglable { mutating func toggle() } enum OnOffSwitch: Togglable { case off, on mutating func toggle() { switch self { case .off: self = .on case .on: self = .off } } }
  5. Protocol with Extension struct Car { let year: Int let

    modelName: String ... } extension Car: FullyNamed { var fullName: String { return "\(year) \(modelName)" } } Adopting Protocol Retroactively protocol FullyNamed { var fullName: String { get } func printFullName() } extension FullyNamed { func printFullName() { print("It's name is \(fullName)") } } Providing Default Implementation
  6. Conditional Protocol Adoption extension Array: TextRepresentable where Element: TextRepresentable {

    var textualDescription: String { let itemsAsText = self.map { $0.textualDescription } return "[" + itemsAsText.joined(separator: ", ") + "]" } }
  7. Inheritance, OOP One superclass — choose well! No retroactive modeling

    Superclass may have stored properties • You must accept them • Initialization burden Should know what/how to override (and when not to)
  8. class Ordered { func precedes(other: Ordered) -> Bool { fatalError("implement

    me!") } } class Number: Ordered { var value: Double = 0 override func precedes(other: Ordered) -> Bool { return value < (other as! Number).value } }
  9. class Ordered { func precedes(other: Ordered) -> Bool { fatalError("implement

    me!") } } class Number: Ordered { var value: Double = 0 override func precedes(other: Ordered) -> Bool { return value < (other as! Number).value } }
  10. protocol Ordered { func precedes(other: Self) -> Bool } class

    Number: Ordered, Printable, ... { var value: Double = 0 func precedes(other: Number) -> Bool { return self.value < other.value } }
  11. protocol ViewWithBorder { var borderColor: UIColor { get } var

    borderThickness: CGFloat { get } init(borderColor: UIColor, borderThickness: CGFloat, frame: CGRect) } extension ViewWithBorder where Self: UIView { func addBorder() -> Void { layer.borderColor = borderColor.cgColor layer.borderWidth = borderThickness } } extension UIButton: ViewWithBorder {} extension UIImageView: ViewWithBorder {} button.addBorder() imageView.addBorder()
  12. Mixing Value Types and Reference Types Maintaining value semantics requires

    special considerations! • How do we cope with mutation of the referenced object? • How does the reference identity affect equality? Unrestricted mutation of referenced objects breaks value semantics. • Non-mutating operations are always safe • Mutating operations must first copy
  13. Swift is the latest language that is already popular and

    backed up by giant brother, Apple. So, it’s worth having POP and Value Semantics in mind!