Monday, June 13, 2005

C# delegates Vs Java Listeners

When I was first introduced to delegates, I found it quite confusing compared to the Java Listener concept. As we know, Java Listeneres use the classic Command design pattern to implement call-backs (implement a standard interface and the method e.g. OnMouseClick())

But after understanding delegates, I have realised that delegates are a bit more flexible than Java Listeners. By using a delegate, U are free to use any method of any object- just the signature of the method should match.
They allow easy binding of event handlers and other callback mechanisms, without the use of a separate class implementing a specific listener interface (which is the Java approach for callbacks).
Basically, instead passing a callback function via an object of known interface that both communicating parties agree on, delegates only need the two parties to use the same function signature

An interesting article regarding this can be found at:
http://blog.monstuff.com/archives/000037.html#more