I will try to explain the meaning of delegation with one example.
Say you have a table; the table will show some rows. Suppose now that you want to configure how this table responds to an event, such as selecting a specific row.
One common way to do this in OOP is to subclass the base class of the table and override some methods there.
When delegating, you do not need to subclass the base class of the table; rather, you use the base class and tell it to "forward" some messages to another object. This is the main idea.
In our example, when a row is clicked, the base class of the table does not know what to do, except send a message to one object that you specify as a delegate to transfer this action.
So, one of the main benefits of delegation is that you do not need subclasses. Another advantage is that the delegate can act as a delegate for several other objects. Indeed, if you look at the general declaration of the delegate method, you will see that the first parameter is the object that delegates:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
therefore, when a delegate receives a message, he knows who sent it and what object it should interact with.
The example I gave you in the table shows one kind of delegation that I would not compare with callbacks. In any case, you can use delegation, as well as some advanced callback scheme.
Take the NSURLConnection class; it can be used to control asynchronous communication.
Asynchronous communication is a typical case when callbacks are used.
With NSURLConnection , a delegate pattern is preferred; so instead of specifying a callback (a function that should be a static function or a method of a static class), you specify an object. This object implements the methods that the protocol defines ( NSURLConnectionDelegate protocol); you can see them as a whole set of callback functions. When the NSURLConnection has some ready-made data, it will call an interface method, for example, โ connection:didReceiveResponse: to indicate that it received a response.
In this case, the point does not exclude subclasses; rather, it is more flexible callback mechanisms (mainly for better encapsulation).
Hope this helps clarify two concepts ...