When I Say Delegate, You Say What?
A short journal about implementing delegation pattern in iOS Development
If you have been working with TableView or CollectionView, you must have heard of the term delegate or maybe have implemented it. But what does delegates exactly do? and can you make one yourself? This article is an attempt to oversimplify delegate explanation so that you can understand it and make one for yourself.
Let’s start with a quote based on Apple documentation:
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object — the delegate — and at the appropriate time sends a message to it.
The easiest real life delegation relationship we can refer to is a relationship between a boss and employee where one acts on behalf of the other. Let’s describe the relationship in code.
Let’s create a fictional class Boss which has doSpeech() function that when invoked makes the boss do a motivational speech for his employee. And then take another fictional class Employee which has doApplause() function that— you guessed it — makes the employees give the most enthusiastic applause it can give.
Now the fictional time has come for the boss to doSpeech(). The boss has pulled an all-nighter to prepare for this moment, hoping his employees can be more motivated after hearing his words.
A moment went by, a perfect speech has been heard by the employees. But something feels wrong, nobody did the applause correctly. Some did the applause in the middle of the speech, some before the speech even began, and the rest didn’t even know when they should give applause. The boss cries at the corner, knowing his employees isn’t capable of giving him an applause that is ‘just right’.
Who is to blame? The employees? Of course not! Let’s just blame the system like we always did!
Just kidding~ The boss is to blame because he never tells the employees about the right time to doApplause(). And of course this problem can simply be solved by telling the employee to doApplause() when the boss wants them to do applause.
After doing proper training, the boss is now happy that the employee knows when to clap. YEAH! Mission accomplished! However though, we haven’t mentioned protocol at all, quite an irony for such a title ain’t it?
Now let’s talk about it. Let’s give the boss another problem, what if the boss wanted to doFollowUpSpeech() after the employee gave a huge applause? The boss have no idea when the applause is finished, he would never want to miss the right timing for a follow up speech!
Maybe the employee should broadcast a notification, put it on NSNotificationCenter and let the boss listens to it so that he knows the right timing for a follow up speech. But knowing the Boss is a huge sentimental person, he doesn’t want anybody else to know the exact timing of the applause to finish, he doesn’t want somebody else pops up, takes over his stage, and gives a better follow up speech. What a bother!
Little does the boss know, the employees has a secret protocol only known for someone who earned their trust, someone who can represent them, someone who can delegate them! (This is a bit forced, I know)
Better yet, whoever implements this protocol, for instance: Manager, Doctor, Custodian, Bystander, President, and whoever earn the employees’ trust is able to precisely know when the applause is finished! No more awkward follow up speeches! YEAH.
Now all the boss has to do is earn their trust, know their protocol, implement the protocol, and wala~ the boss can now have a paid leave happily ever after. Psyche! You wish boss! You have to do some speech for the investors now!
Thanks for reading this! This is only an oversimplification of delegate protocol implementation in Swift and actually a bad example of real life delegation, when does boss ever become a delegate for its employees, the reverse relationship maybe is more plausible. I’m open to better implementation and critiques and will gladly write follow up articles regarding the corrections.