Saturday, August 18, 2012

Why,What and How "Delegate" (Part -1)


My apologies if you are waiting for the LINQ stuff as promised in my previous post. Those are on the way. In the mean time please have a look at the concept called "Delegate".

With your permission I'm starting.
 
I think when any one reads delegate, the first doubt he might get is, "Why all this non-sense ?". We can directly call the required method. Why this loop line ? If you are also getting the same feeling then let me tell you, you are not alone. Just Google "Why delegate ?". You will be getting 10's of links.

When i thought of writing about delegate, i was bit hesitant because i felt my article will become one among many of the already available unclear content on this subject. Still i decided to go ahead. If you feel something is clear after reading my attempt, take it otherwise sorry for wasting your time.

Thing is, delegate is not complex. It is a simple concept. Only problem is you might not found many scenarios which demands using it. That's why it looks alien or unknown or can be ignored concept.

I do not want to give any theory or definition part of it. I would like to go with practicals. I will give you two examples. If you need theory then I'm sure you won't appreciate this post. If you have done some initially study on delegate and not clear or confused about it then these examples may help you.

Here we go.

Example 1 : I want to steal your method

Mr.Nokia is a software professional.

One Sunday, he thought of designing a new class and declared it like below


Before writing any other code, he went to toilet leaving his class like this. His wife saw the name of this class and without any reason (as usual) she got angry on him and written one method like below.



Mr.Nokia came back from the toilet, got really angry by seeing it but could not say anything. He quietly kept this method as is. But he decided, he should never call this method from any application.

Here came the twist.

One day his sister rang him and asked him for a similar method to take revenge on her husband family. Now, he realized the importance of the method his wife has written.But still he doesn't want to call it.

After some thought he did like this.

He created similar method like his wife's method. He followed that signature of both the methods absolutely same. Otherwise, he knows that he cannot call it.



Then he declared variable for this definition



While declaring it, he did this intelligent thing. He has given address of his wife's method for it. His sister can use this method without knowing who has created it. At the same time he is also satisfied as he is not touching his wife's method as he hates it.

Finally, he has written the method which his sister actually requested for. She only knows about this method. She doesn't know that this method has no functionality inside and it has been 'delegated' internally to some other method.


Just recap the whole thing in one single hit


As you can see from the above image, it involves just three steps. High light is the second step. There Mr.Nokia can give address of any method as long as signature of that method is matching with the thing declared in Step 1.

Now Mr.Nokia's sister can use this class in her application and can call this method like below.

 
That completes our first example. Is it clear ?
Shall we move on to the second example ?
Yes ?

Not now yaar. I'm exhausted. Please wait. I will come back with my next post with the second example.

Bye for now !

Take care !!

No comments:

Post a Comment