If you’ve spent some time working in iOS in Swift, you may have noticed that you see the word Delegate everywhere. Delegation is a powerful pattern that can be really useful, and it’s easier than you might think to create your own delegates.

In thinking about delegates, a good way to explain them might be a metaphor of a wedding. At a wedding, there’s a ceremony, some sort of music and dinner gets served. If we made a class to model that, it might look something like this.

So, we’ve got all the basics covered, but this code isn’t reusable. In a real wedding, sometimes everything comes with the venue, but other times it’s provided by outside vendors. That’s exactly the same function a Delegate does for us. In Swift, starting with a protocol for your delegate is a good idea, since it lines up well with the function of a delegate.

Let’s start by making our protocols. Protocols are a cool way of saying that anything that conforms to this protocol will implement the stuff here. It’s basically swift’s way of saying, “Hey, I promise that if you meet a Insert name of Protocol here, that it will be able to do X, Y, Z .” The rest of the program or class can then depend on the delegate to definitely have those features.

So, like I said, to “Delegate-ize” our class, we need three delegates, Ceremony, Music and Food.

So, our Music Delegate needs to be able to play music and take song requests and return true or false that they will be accepted. Our food delegate needs to provide some sort of food, and our ceremony delegate is responsible for performing the vows.

First let’s make the ceremony delegate and modify the venue class.

Okay, we added the CeremonyDelegate protocol, and modified the class. Basically instead of just having it say “Do you, do you?” we can set it up so that the ceremony can easily be provided just by replacing the delegate instead.

So, now, maybe we could have the guy from the princess bride as the officiant.

Cool. Let’s add the other two delegates:

So, with all that finished, we change our wedding venue class to this:

We now have 3 delegates who are going to handle all of the different functions, just like in a wedding we might have a caterer, a DJ and an officiant. Instead of hard-coding in all of the default values, we are going to defer to our delegate to handle all of the different delegate functions. Let’s put all of this to some good use now by adding a MusicDelegate and a FoodDelegate .

Since we now have classes that conform to all of the different delegates, we can now put them to use.

Now, immediately there are a few benefits to doing this in our code.

Classes can become reusable and more generic when we split out the code that handles the heavy lifting for specific features. Splitting some of the logic out of the main class allows for smaller classes as the different responsibilities are separated out and each delegate is only responsible for some of the code. Having code with a distinct concrete job makes it easier to reason about the code and to modify. Making a delegate can make it easier to change your code later. For example, let’s say you were using something like Parse or Firebase as the backend for your app, but later decide to roll your own server. If you’ve made a NetworkServiceDelegate , then all you have to do is make your new network code conform to the new delegate protocol and won’t have to modify the rest of the code.

To see how it all works, try copying the code below into a playground and messing around with it.

This is just a silly example, but imagine this could be a table that is re-usable for different types of media like videos with a MediaDelegate, responsible for returning information about / presenting media. You can easily change the behavior of a class without having to completely all of the logic that handles it. This is exactly how UITableViews/UICollectionViews work with their delegates and data sources and we can harness this power ourselves.

Hopefully this helps give you some ideas about the delegate pattern and some thoughts about how you might be able to use it. What are some other ways that you use the delegate pattern in your programming?