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

Visualising Protocols - Swift Mumbai #7

Visualising Protocols - Swift Mumbai #7

Speaker: Viranchee Lotia
One of the apps that I worked on as an intern had a block of code copy pasted in various view controllers. It is a good technique until when code reuse blows up, and you need the same functionality in multiple classes. One of the approaches to solve is taking that piece of repeated code out to a Protocol. Implementing a certain class to complete that functionality leaves us with only one point of failure/testing and eases maintainability of the code.

GitHub - https://github.com/viranchee
Twitter - https://twitter.com/code_magician

Presented at Swift Mumbai Chapter 7 Meetup hosted by BookMyShow
https://www.meetup.com/Swift-Mumbai/events/258465693

Swift India

February 23, 2019
Tweet

More Decks by Swift India

Other Decks in Programming

Transcript

  1. PARENT - CHILD RELATIONSHIP ➤ Child has all properties of

    parent ➤ Child can override parent properties ➤ Visual: Vertical organisation Parent Child Human Coder Write Code Climb Walls
  2. ACCESSORIES RELATIONSHIP ➤ Add additional functionalities to existing class, struct,

    enum ➤ Visual: Horizontal organisation ThisClass Protocol A Protocol C Protocol B Human Climb Walls Write Code
  3. DELEGATES: HANDOVER RESPONSIBILITY ➤ Simpler classes ➤ Abstract code out

    of the Classes ➤ Massive View Controllers? Design Patterns to the rescue!
  4. //PLAYGROUND 2: BASIC DELEGATION Protocol Class weak var Delegate: Protocol

    Delegate Class func download () -> () var delegate: Protocol? = DelegateClass() delegate.download()
  5. “➤ “You should also be careful about creating retain cycles.

    Most often, delegate properties should be weak. If an object must absolutely have a delegate set, consider adding the delegate as an input to the object’s initializer and marking its type as forced unwrapped using ! instead of optional via ?. This will force consumers to set the delegate before using the object.” -Joshua Greene. “Design Patterns by Tutorials”
  6. CODE REUSE BY CTRL V ➤ Want to reuse functionality?

    ➤ Copy and Paste ➤ Abstract out the required functionality, this time we use delegates ➤ Optimum strategy: Works fine until pasted for the 3rd time, then delegate it