Re: notifications vs delegates (was: The fuzzy aspects of Cocoa)
Re: notifications vs delegates (was: The fuzzy aspects of Cocoa)
- Subject: Re: notifications vs delegates (was: The fuzzy aspects of Cocoa)
- From: "John C. Randolph" <email@hidden>
- Date: Wed, 27 Feb 2002 12:34:02 -0800
On Tuesday, February 26, 2002, at 07:05 PM, Jay Kuri wrote:
Thanks for the info... just a couple more questions...
Suppose you have an object (part of a framework) that wants to
deliver a small chunk of data to an interested object.
Normally, this data would only be of interest to a single
object... is there a reason to choose delegates over
notifications or vice versa?
Well, in this case, you would probably just send a message to
that interested object. A delegate is an object that tells
you how to modify your behavior.
Ok... In the particular situation I'm thinking of the object
sending the messages (or posting the notifications) is part of
a network-messaging framework. The object that should be
informed of the new 'request' message arriving will not be
known when I write the framework... and different types of
messages could have different object handlers. Again, this
would vary depending on the app.
The two ways I can see doing it are:
1) The app registers one (or several) of it's objects to
receive notifications and the framework builds the notification
names dynamically.
I don't see why the *names* of the notifications need to change.
or 2) Have the framework use a dictionary or other container
holding a series of (id *)'s and using an informal (or formal)
protocol to deliver the messages via method calls. (of course,
the objects would have to register themselves with my object in
this case)
..in which case you're re-implementing the
NSNotification/NSNotificationCenter classes.
The simplicity of implementing the notification method appeals
to me... and the way the network-messaging protocol works is
very analogous to notifications in cocoa... (requests arriving
that may or may not cause something to happen, depending on the
app) It seems like Notifications are a natural interface...
what I'm trying to figure out is: is this a 'bad' way to use
notifications? are they too slow to be useful for processing
this kind of input?
How would you do it?
Probably the way that NSTask does. You send a notification
whenever one of your packets arrives.
From what I gather, notifications are a bit more flexible in
terms of dynamic messages (notifications with names that are
defined at run time) but delegates are faster...
Yes. When you post a notification, you don't need to know or
care if anyone's listening for it.
This is acceptable, I wouldn't want the framework object to
do anything with messages the app didn't want to deal with....
In fact, the framework object wouldn't dispatch messages the
app didn't say it wanted, regardless of how it was implemented.
Thanks again,
Anytime,
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.