Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've a simple question about the terminology used in the Delegate pattern;

Let's say I create an object that will be responsible to download something from the Net and when it is finished, will call me back with the result. I guess we can call the downloader object the "delegate", since he is doing work on the behalf of my main object.

If I'm right 'til here, then, I've a problem with the sample codes I read. The property in the Downloader Class is often named "delegate", and put to Self by the creator of this downloader.

Am I wrong or it should effectively be called "delegator" instead ?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The delegate is the thing that is performing the sub-task. In your case, Downloader is doing the main task, but then tells the delegate to perform some task. Downloader does not know or care what that task is, just that it has to happen.

If you have to use delegator, it would be Downloader as it is the class that delegates work to something else. But I think that runs into problems because you have multiple words that are similar and lead to confusion like yours. Instead, I tend to think of things in terms of events and listeners. When Downloader finishes, it fires a downloadFinished event and tells any listeners that the event occurred. The listener than decides what it wants to do with that information.

share|improve this answer
    
Does it mean that I really get the pattern upside down ? To me, the main object is doing the main job, and ask to the "downloader" to do a side job and call him back when it's done. The main object creates a delegate, in that sense. But it seems I 'm wrong, isn't it ? –  Pierre Apr 29 '11 at 15:37
1  
@Pierre: It all depends on the scope of what you are thinking. Main is doing a task and creates a Downloader to do a side task. But from the scope of Downloader, it is doing a task and notifying someone else when it finishes. Main doesn't care about what is inside Downloader and Downloader doesn't know Main exists. It is ok if they both have a delegate variable that do different things. –  unholysampler Apr 29 '11 at 15:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.