Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will try to explain the value of delegation with one example.</p> <p>Say you have a table; the table will show some rows. Suppose now, you want to customize the way this table react to some event, e.g., the selection of a specific row.</p> <p>One common way of doing this in OOP is subclassing a table base class and overriding some methods there.</p> <p>With delegation, you don't need subclassing the table base class; rather you use the base class and tell it to "forward" some messages to some other object. This is the basic idea.</p> <p>In our example, when the row is clicked, the table base class does not know what do except sending a message to one object that you specify as the delegate to carry through that action.</p> <p>So, one basic advantage of delegation is that you do not need to subclass. Another advantage you have is that a delegate can act as the delegate for several other objects. Indeed, if you have a look at the generic declaration of a delegate method, you will see that the first parameter is the object that is delegating:</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath </code></pre> <p>so when the delegate receives the message, it knows who sent it and with which object it should interact. </p> <p>The example I gave you about the table shows one kind of delegation, which I would not compare to callbacks. Anyway, delegation can be used as well as a kind of advanced calling-back-scheme. </p> <p>Take the class <code>NSURLConnection</code>; it can be used to manage aynchronous communication.</p> <p>Async communication is the typical case where callbacks are used.</p> <p>With <code>NSURLConnection</code>, the delegate pattern is preferred; so, instead of specifying a callback (a function, which has to be a static function, or a static class method), you specify an object. This object implements the methods that a protocol defines (<code>NSURLConnectionDelegate</code> protocol); you can see those as a whole set of callback functions. When the <code>NSURLConnection</code> has some data ready, it will call a method of the interface, e.g., <code>– connection:didReceiveResponse:</code> to notify that it has received the response.</p> <p>In such case, the point is not avoiding subclassing, rather it is a more flexible callback mechanisms (allowing for better encapsulation, basically).</p> <p>I hope this helps clarifying the two concepts...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload