Spodworld

a collection of technical fixes and other randon stuff

What the f**k is a 'Delegate' in programming?

I have rarely come across a good article on delegates, and after 20+ years in programming I'll have a go at explaining it myself.

I've often struggled to understand the concept, it's use, benefits whilst actually having implemented them quite well.

Q: "...so what the fuck are delegates?"

A:
"A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance"

Q: "...so what the fuck are delegates?"

 

...Okay... you get my point!!!??? When you explain it in programming language, it kind of loses it's practical meaning (unless you eat, sleep, breath coding.)

Q: "...so what the fuck are delegates?"

 

Well firstly, it helps if you are familiar with the concepts behind: methods,events and reflection and multi-threading.
You only need a little knowledge from each area to follow this article further, namely:

  • You should be aware how to write a method or function. If not go away now, why the hell are you reading this article?
  • You should know that,  with little code, some pretty clever things can be done using reflection, and you can manipulate objects based on what information your code knows about it's type and properties.
  • You should know multithreading is a way of firing off lots of processes at once to hopefully speed things up.

 

Lets go back to the answer i copied from wikipedia earlier and give an example.

say we have two functions:

string HowExplosive(string animalName, string vegetableName)
{
}

and

string HowTasty(string animalName, string vegetableName)
{
}

Both these functions take 2 strings as parameters and return a string as their output value.
They have the same characteristics, so we could describe all methods that have these characteristics by giving them a type.

Let's give them a type of DumbAssSillyFunctions. Lets code it!.....

public delegate int DumbAssSillyFunctions(string animalName, int vegetableName);

and there we have it..... a delegate....it tells us all DumbAssSillyFunctions
take 2 strings , one for animal name and one for vegetable name. And they also return a string.

Having established a way of classifying methods with this delegate/type we've got ourselves some information about our code. Information about code is how reflection works so you should be thinking that we have an opportunity here to reduce code and handle multiple situations.

You should also know that events are very similar to functions and so could also be described using a delegate statement, which often happens.

Are you understanding yet?  Now why did i mention reflection earlier????

Consider this.... your program is receiving inputs of different combinations of animals and vegetables..

Cow, Carrot
Sheep, Turnip
Lamb, mint

etc....

WE COULD USE DELEGATES TO CALCULATE a)HOW TASTY THEY ARE, b) HOW EXPLOSIVE THEY ARE IN SOME SORT OF LOOP!!

Lets take this to the extreme...suppose we wanted to analyse every vitamin and mineral content for each combination... We could hard code all the method calls....or....... we could use reflection to loop through all methods available and find the methods that are of type

DumbAssSillyFunctions.

When we find a function that matches, we know to pass the animal in first and the vegetable in second, and we will get a result.
This lets our loop execute without knowing anything about the function names or how many there are.
....so we can add more features without breaking or having to modify the main loop.


This is an example of one routine calling many, and we can see how this can be used to launch multiple threads in a multi-threaded application.

Now lets reverse it and see how we can have multiple things calling one method to implement code re-use. Suppose we have some "FoodComboAnalyser" objects.
If we gave all these events of a delegate type "CrazyAlert", one of these may fire if the animal an vegetable are too explosive. The other may fire if it is particularly tasty.
We may want to log these down or show a message when they occur.

We can do this by assigning a handler to the events. The handler will also have a matching delegate type. Once linked, the event handler catches the events and carries out the actions on the information passed to it.
This is how a lot of visual programming languages work by handling onclick, onfocus, on keypress etc...

Continuing the handling of events... an extreme example of event management is the Node.js framework, and i recommend the "codeschool.com" tutorial on it.
Here they explain the event loop which constantly listens for events and handles them by passing the data onto processing functions.


Watch it and you may note, that it is a pretty impressive way of handing out work. I likened it to a super-duper Project Manager handing out lots of work to other people...

or delegating ...to delegates...

Has it clicked yet? :)


 

 

 

Loading