Fundamental Design Pattern: Delegation


Design Patterns that will be covered in this article:

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


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


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.


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








iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Core Location — How to Display a Human-Readable Address Using CLGeocoder

Swift Finance 2.0 Launch Plan

Getting started with iOS Widgets — Part I

How to fix Flutter stuck at “running pod install…” when running on vscode building on simulator

How to fix Flutter stuck at “running pod install…” when running on vscode building on iPhone simulator.

Demystify SwiftUI

Why SwiftUI is the best UI framework across Apple platforms

The Difference Between ClipsToBounds And MasksToBounds

Ios 10 Rom Zip File Download For Android

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
John Kim

John Kim

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio:

More from Medium

Fundamental Design Pattern: ModelViewController

iOS: Certificate pinning Part — 2 (GraphQL)

UserDefaults Unit testing in iOS

Swift Dictionary and Set and its time complexity — Data Structure