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

2. Perform a task rather than do the task itself

  • There are 3 parts to the delegation pattern
  1. An object needing a delegate (a.k.a the delegating object)

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

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

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

  • 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

Main Takeaway

  1. The delegation pattern has 3 parts in-total
  • A delegating object
  • A delegate protocol
  • A delegate

2. This pattern’s objective is to break up large classes and create reusable components

3. Generally, Delegates should be weak properties in most use cases

Conclusion

In case if you haven’t yet, join my Discord server! I’m looking to create a community of developers so we can reinforce, motivate, and get to know each other in our programming journey.

Discord

If you have any questions or comments, please don’t feel afraid to ask or connect with me on social media! You can also send me an email at jkim@cloudiosx.com

LinkedIn

AngelList

GitHub

Portfolio

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store