Delegation is distinct from interfaces & far more powerful. It is
part of the runtime as you mentioned, but Cocoa==Obj-C like
JavaVM==Java. Java actually took the concept of interfaces from
Objective-C where they are called protocols.
I never said that delegation and interfaces are the same thing. I
said that delegation can be implemented using interfaces.
IMHO, Interfaces are a poor way to implement delegation; once fixed
they cannot be easily extended without breaking client code.
Karl
Karl's comment got me to re-evaluate my understanding of the delegate
mechanism in Cocoa and I think I was a bit too quick in my statement
that delegation can be *easily* implemented with interfaces.
Niels is right in that delegation is a more powerful concept than
that of interfaces, and so is Karl in that interfaces are a poor way
to implement delegation.
For the benefit of others who, like me, are experienced Java
programmers but Cocoa newbies, let me elaborate a bit on this.
Cocoa's delegation mechanism does not *require* a delegate object to
implement a particular delegate method whereas Java interfaces are a
strict bonding contract between the method caller and the interface
implementer.
As a result, adding a new method to an interface will require clients
to change their code, even if they're not interested in actually
using the new method. Cocoa delegate clients couldn't care less and
won't *have* to change.
As it turns out, there's a way around, namely, using the Adapter
pattern. The problem with Adapters, though, is that they're not
interfaces but actual classes, and that forces a delegate to belong
to a fixed class hierarchy.
On the flip side, protocols (the Obj-C analog of interfaces) aren't
binding, so you can't be *guaranteed* that a class implementing a
particular protocol will implement *all* of its methods.
It seems to me, then, that there's room for improvement in both
languages. A new language might have three distinct kinds of types:
classes, interfaces, and delegates. Classes force a particular
hierarchy, Interfaces impose a bonding contract, and Delegates form a
relaxed interface, not requiring its methods to be implemented.
Thank you, Karl, for getting me to rethink my position. :)
Wagner
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden