Skip to content

Instantly share code, notes, and snippets.

@cloudiosx
Created Mar 23, 2022
Embed
What would you like to do?
Fundamental Design Pattern: Delegation

Introduction

Design Patterns that will be covered in this article:

  • Delegation

Delegation

  • This pattern enables an object to use another helper object to either:

      1. Provide data
      1. Perform a task rather than do the task itself
  • There are 3 parts to the delegation pattern

    • An object needing a delegate (a.k.a the delegating object)
    • A delegate protocol, which defines the methods a delegate may or should implement
      • An object that can implement the protocol qualifies to be used as the delegate
    • The delegate (a.k.a. the helper object that implements the delegate protocol)

When Should I Use This?

  • You should use the delegation pattern when you want to break up large classes or create generic reusable components.
    • Datasources and delegates are both examples that follow the delegation pattern as each involves 1 object asking another to provide data or do something for it
      • Datasources provide data
        • UITableViewDataSource (numberOfRowsInSection, cellForRowAt)
      • Delegates receive data
        • UITableViewDelegate (didSelectRowAt)

Code

RestaurantViewController.swift (Delegate)

Screen Shot 2022-03-23 at 3.03.09 AM.png

  • RestaurantViewController is a simple table view controller that has only 1 cell labeled as "Grocery Store". We also notify the view controller that a segue is about to be performed with the prepare method and this is where we set the FruitViewController's delegate to be the RestaurantViewController.
  • Because RestaurantViewController is serving as the delegate, RestaurantViewController can conform to the delegate protocol (FruitViewControllerDelegate).

FruitViewController.swift (Delegating Object)

Screen Shot 2022-03-23 at 3.04.19 AM.png

  • FruitViewController is another controller displaying a table view except that it was configured a bit differently than RestaurantViewController. Instead of having the class directly inherit from UITableViewController, we used a standard view controller and added a table view from the Object Library directly on top of the view controller. We made sure to set the table view's delegate and data source to be the FruitViewController, which also could be configured in Interface Builder.
  • More importantly, we created a delegate protocol called FruitViewControllerDelegate, in which any delegate may implement the protocol's fruitViewController method.
  • Lastly, I wanted the delegate's method to be called when the right bar button item "Return" is tapped.

Main.storyboard

Screen Shot 2022-03-23 at 3.05.11 AM.png

Main Takeaway

  • The delegation pattern has 3 parts in-total
    • A delegating object
    • A delegate protocol
    • A delegate
  • This pattern's objective is to break up large classes and create reusable components
  • Generally, Delegates should be weak properties in most use cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment