Fun with delegates

Published 11 April 04 09:50 AM

This last month, after over three years of working with .net, I finally understood what a delegate is. Now understand that I could correctly use the syntax and setup event handlers and other common uses of delegates before then. Still, I just didn't get it. Part of it is that the grammer used with event handler examples confused me. A delegate is Command *object*. In the examples, the delegate didn't seem like a thing, a noun, an object. In retrospect, I can't really say why I was confused now that I have had the light turned on for me. On the other hand, look at of the examples most of us are exposed to:

this.Button1.Click += new System.EventHandler(this.myEventHandler);

In that statement there is nothing obvious that indicates that an EventHandler is a delegate. It's defined somewhere else and appeared to me as some voodoo related to the .net event handler framework. It does look like a thing though, a noun, but somehow it reads like, “use this method as the handler for the Click Event of Button1“.  Which is precisely what it does say, but it is not clear that myEventHandler has magically become an object in it's own right.

public delegate void Fireable();

OK, I'm going to go out and get me a Fireable. Sounds more like an adjective. the gun is fireable. An adjective can't really stand by itself. Now if you are designing a gun, or something that can fire an event the signature probably makes some sense:

public class Gun : Weapon, Fireable

or

public class Window : Fireable

So, a gun or a Window could be described as fireable. Now, does this make sense?

public Shooter.Shoot(Fireable fireable)

Not really, I want the thing that shoots, not a description of the thing that shoots. What I would expect is something more like:

public ShooterShoot(Weapon weapon) or maybe something like

public delegate void FireCommand();

Here is another example of something that led me from the true path:

private void OnClosing(
    Object sender,
    System.ComponentModel.CancelEventArgs e)

Now this is a method that will passed somewhere else as a delegate. But methods should be verbs, like Close() so on a window, a window can Close, but how does a window OnClose? A window could have an event like Closing, Opening etc. Does that look like an object? A verb? Adjective?

It was Bruce Eckel's blog about “latent typing“, as done in python, that finally turned the light on for me. In that case, he was referring to the way generics are implemented in java and c#. Internally in these implementations, the actual type is the lowly object, but externally, the implementation requires you to name, and implement an interface that the generic will be typed as. In python, it seems the only restriction to what can be added is that the signatures match. He calls that latent typing and goes on to argue that it is just as safe as the strong typing enforced by the compiler in java.

Wait just a minute, an implied type inferred by a signature, that's what a delegate is! And of course c# has this syntax coolness that in this one case allows you to refer to a method name directly instead of having to go back to reflection to get a MethodInfo, which is already an object that represents the method.

The Command pattern is a very powerful tool in object oriented design. And, better yet, it's simple enough that anyone can grasp it. And c# gives this pattern a first class implementation. Now if only I had understood this from all the examples I had seen. Seems sooo obvious now, but I wonder if I am the only one that missed the point.

 


 

Filed under:

Comments

# panmanphil said on April 18, 2004 6:19 AM:
Hi Philip,

I can't wait until you write more about this! VERY interesting!

Best Regards,
Jimmy
http://www.jnsk.se/weblog/
###
# panmanphil said on April 18, 2004 2:55 PM:
I have to say that I read this and thought: Yep, Delegates are cool and then moved on ;-)
Then Jimmy mentioned your post in an email and we got to talk. The point that you can define the behavior of an object by inheriting from Delegates instead of through the implemenattion of interfaces
(public class Gun : Weapon, Fireable) is indeed a very poweful concept!
It basically allows us "weave in" the behaviour of an object at run-time by specifying the target method of the Fireable (AOP like). One problem with this approach compared to interfaces though. We don't have multiple inheritance so we can only inherit from one Delegate class :-(
Maybe the Delegates should become encapsulated in interfaces like this:
public class Gun : IFireable, ICleanable

public interface IFireable {Fire();)
public interface ICleanable {Clean();)

The constructor of the Gun could then be supplied delegates for the Fire() & Clean() methods. These delagtes could point to "whatever-runtime-loaded-code" we wish and we would thus be able to supply the object with behaviour at runtime. I'm not sure if this is cool or dangerous...but it is at least interesting ;-)

/Chris

# panmanphil said on April 19, 2004 4:40 AM:
I haven't had time to really explore this, except in my head while driving and the like. Yes, inheriting from Delegate would be a problem, actually, I'm not sure what it would give you. Interesting in it's own right. But I was thinking more on the lines of burying the delegate in a generated implementation class. Whether done by a framework at runtime so you would use

Framework.Create(MyType)

or at compile time with a custom build action is still a big question. I would like to prototype some different ideas and see what feels best.
# TrackBack said on November 12, 2004 3:56 PM:
# generic viagra said on March 21, 2007 7:14 AM:

news

# cheap tramadol said on March 21, 2007 8:50 AM:

news

# cialis said on March 21, 2007 7:28 PM:

news

Anonymous comments are disabled