DevPrep

All topics › Design patterns

Delegation

Junior PRO

Essence

Delegation is a pattern in which an object hands part of its work or decisions to another object, the delegate, through a protocol. The object does not know the delegate's concrete type, only the contract. It is one-to-one, unlike Observer: a UITableView has one delegate and one dataSource. Almost all of UIKit is built this way: UITableViewDelegate, UITextFieldDelegate, URLSessionDelegate, UIApplicationDelegate.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store