4

I have done a fair bit of reading and I am at the stage where I am beginning to grasp what they do, but am at a loss when it comes to why or where I would use them. In each example I've seen the recurring definition seems to be as a method pointer, and you can use this in place of a call to the method which is apparently useful when the developer doesn't know which method to call or the selection of a method is based on a condition or state.

This is where I struggle a bit, why can't I just have an if statement or a switch statement and then call the method directly based on the outcome? What's so bad about calling a method directly from an object instance? From my understanding a Delegate offers a better way to do this but I can't understand what's better about it, from my perspective it's just a round-about way to achieve the same thing an if statement could do when deciding which method to call.

I'm at a loss and have been rambling on for quite a bit now, any help at all on the matter would be greatly appreciated!

mike
  • 2,073
  • 4
  • 20
  • 31
  • possible duplicate of [Where do I use delegates?](http://stackoverflow.com/questions/31497/where-do-i-use-delegates) – JasonWilczak Jul 28 '15 at 14:37
  • 1
    Many times you *don't* know which method to call. For example, a Sort method doesn't know the criteria by which you want to order a collection of objects. In this case you can pass a pointer to a function that receives two objects and decides which comes first. Another case is event handlers - you don't know who is going to subscribe for events, so you allow callers to register delegates that your code will when appropriate – Panagiotis Kanavos Jul 28 '15 at 14:39
  • 1
    Well, delegates shorten code. I'm using android client to post this so I don't know if this is going to show up as SO code. Anyway for example look at this Dispatcher.Invoke((Action)delegate { // stuff here }); It uses less byte space and it is easily readable. It has other advantages too, not going to write about them since I'm using android client, I believe someone will give a fine answer to this. – Yytsi Jul 28 '15 at 14:41
  • 2
    Because using a delegate over a direct method call allows the caller to be independent of the method call. Think of it as someone calling a pizza place... you're calling GetPizza()... the dispatcher will send you pizza, but it will only use a driver that registered to the delegate function pointer. – SpaceSteak Jul 28 '15 at 14:53
  • @SpaceSteak I think I might have understood it a little better from your example, let me run my current thoughts past you. Without a delegate GetPizza() would be called and the pizza place would then have to go through each of it's drivers to check if anyone is able to deliver the pizza. With a delegate the checking through all the drivers effort is removed and the delegate handles this task and delegates the pizza delivery to a registered driver. So, the Delegate acts as a layer on top of the class and directs incoming messages to the correct method? (if I have understood correctly) – mike Jul 28 '15 at 15:27
  • 1
    @tonedlemming Essentially, yes... although it's a layer on top of a function. And what happens is all the drivers listening get the incoming message... ok so taxi service would have been a better example. But yeah, essentially delegates are an abstraction to direct incoming function calls. GetPizza() calls the delegate, and any driver, cook or store can "hook into" the delegate, without the GetPizza() caller being aware. – SpaceSteak Jul 28 '15 at 16:50
  • @SpaceSteak thank you very much for the response, your explanation of the actual functions of a delegate has helped a lot. Through understanding more thoroughly what they do has helped me begin to understand where I might put them to use. Although I can't help but see a distinct similarity between delegates and the publisher/subscriber, observer/observable patterns. Would it be correct to make that assumption? – mike Jul 29 '15 at 08:09
  • 1
    @tonedlemming Exactly! :D Delegates are a tool to implement the observer/observable pattern. – SpaceSteak Jul 29 '15 at 11:20
  • @SpaceSteak I think the light bulb has finally switched on! Thank you very much for taking the time to help me out, much appreciated! – mike Jul 30 '15 at 08:13

2 Answers2

3

why can't I just have an if statement or a switch statement and then call the method directly based on the outcome?

This would be fine, if you had 2 or 3 different branches and methods. Now imagine having tens or hundreds of methods which can be potentially called depending on a situation. I wouldn't want to be the one to write that if statement.

Imagine having 100 different potential abilities for a character in a game. Each ability can have its own method to perform. Depending on what abilities a player has, you can just throw those methods into a list for that character using delegates. Now its fully customize-able, and player's abilities aren't hard-written into the code, they can be picked up or lost during the course of the game super easily, and there can be thousands of abilities not to mention the amount of potential combinations.

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
2

Think about it this way. According to SOLID principle of OOD (for example) every object should have responsibility over a single part of the functionality. Using this principle we can assume that:

Classes are responsible for working with custom objects, structs with - sets of data, methods are responsible for actions, events are responsible of signalizing that something happens and delegates are responsible for the corresponding action on this events that should take place.

Events and methods are 'busy' with their own single part of the functionality and therefore cannot handle the events themselves and be responsible for methods. That's why we need delegates...

Fabjan
  • 13,506
  • 4
  • 25
  • 52