Re: Delegates
Re: Delegates
- Subject: Re: Delegates
- From: Jens Alfke <email@hidden>
- Date: Mon, 19 May 2008 12:01:21 -0700
On 19 May '08, at 9:22 AM, john darnell wrote:
As far as I can tell, it is kind of like a virtual function (virtual
because I, the programmer, am expected to flesh it out) that resembles
an event attached to a given class.
You're basically correct, but I think you're overcomplicating the
concept.
A delegate is basically just the OOP equivalent of a group of
callbacks. Many C APIs will let the client set a number of callback
function pointers that will be invoked in particular situations.
(CFReadStreamSetClient is a good example in CFNetwork.) The callback
lets the library tell your code when something happens, or ask it
about something.
In object-oriented programming, this is often done with a delegate.
Instead of callbacks, the library class defines one or more methods
that the delegate should implement. The client then sets the delegate
object as a property, and the object in the library can call the
delegate methods on it as necessary. (Compare the CFStream example
with NSStream, which uses a delegate interface for basically the same
purpose.)
Cocoa is more delegate-happy than most frameworks I've seen. A more
common design pattern is to have the library class define methods on
_itself_ that will be called for this purpose. The user of the class
then has to subclass it and override those methods. It's a similar
idea but it makes the object its own delegate. There are some
advantages to this, but it has the disadvantage of requiring lots and
lots of subclassing in application code, whereas in Cocoa you don't
have to create subclasses nearly as often; this helps keep your code
organized better.
—Jens
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Delegates (From: "john darnell" <email@hidden>) |