Found this fantastic at the same time funny article explaining what are delegates and how they are used. The concept of delegation is simple but confusing at times where it is supposed to be implemented. If you have any trouble in understanding delegation like i did when i started programming in Objective-C 2 years ago. I recommend you to go through this article.
"What the deuce is delegation?
Delegation is a powerful tool in a developers arsenal, i see delegation as a clean and simple way of connecting objects and helping them to communicate with other. In other words, delegation is a dating service for
"What the deuce is delegation?
Delegation is a powerful tool in a developers arsenal, i see delegation as a clean and simple way of connecting objects and helping them to communicate with other. In other words, delegation is a dating service for
Objective-C objects.
If that makes no sense to you, which is likely, Apple puts it a lot better:
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 message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.
Still make no sense to you? Let me try and put this into plain english for you and give you a real life comparison to delegates in action..
Let’s say we have two objects, Brain and Beer Bottle, Brain is the object we use to manage the entire Body application, it handles all of the important tasks such as poop, eat, drink, sleep etc. Beer Bottle is attached to body but it doesn’t know what Brain is thinking, likewise, Brain has no idea what Beer Bottle is thinking.
Brain is using Beer Bottle’s attributes to satisfy itself while it’s watching TV, but the problem is that brain is so distracted by the tv that it can’t pay attention to when beer is going to run out. This could all end in disaster, Brain needs to know when beer is empty so that it send body to the fridge and initialize another instance of Beer Bottle.
Brain can use the drink function to lower Beer Bottles liquid variable, but once liquid reaches 0, Brain needs to know about it, this is where delegates come into action, we can use a Beer Bottle Delegate. Brain can listen out for the Beer Bottle Delegate telling brain that the bottle is empty, all we need to do is simply tell Brain to listen out for Beer Bottle telling it’s delegate that is empty and Brain can react to it. This well thought out and illustrated diagram shows all of this in action:"
If that makes no sense to you, which is likely, Apple puts it a lot better:
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 message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.
Still make no sense to you? Let me try and put this into plain english for you and give you a real life comparison to delegates in action..
Let’s say we have two objects, Brain and Beer Bottle, Brain is the object we use to manage the entire Body application, it handles all of the important tasks such as poop, eat, drink, sleep etc. Beer Bottle is attached to body but it doesn’t know what Brain is thinking, likewise, Brain has no idea what Beer Bottle is thinking.
Brain is using Beer Bottle’s attributes to satisfy itself while it’s watching TV, but the problem is that brain is so distracted by the tv that it can’t pay attention to when beer is going to run out. This could all end in disaster, Brain needs to know when beer is empty so that it send body to the fridge and initialize another instance of Beer Bottle.
Brain can use the drink function to lower Beer Bottles liquid variable, but once liquid reaches 0, Brain needs to know about it, this is where delegates come into action, we can use a Beer Bottle Delegate. Brain can listen out for the Beer Bottle Delegate telling brain that the bottle is empty, all we need to do is simply tell Brain to listen out for Beer Bottle telling it’s delegate that is empty and Brain can react to it. This well thought out and illustrated diagram shows all of this in action:"