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 question about the Delegate Pattern :

If I want to delegate the parsing of a xml file, for instance, that I want to be converted into a Key-value Dictionary. Let's take this classical example...

What's the best practice about the creation of that Dictionary Object?

  • Is it the "main" object that creates it and pass a reference to it to the delegate, asking him to fill it up,

or

  • Is it the delegate that create this dictionary add pass it back to the "main" object, when he has done his job, trough the return value of the call-back method (or any other way)

Is one of this two ways really better than the other ? And why ?

share|improve this question
    
IMO: The delegation pattern isn't an actual pattern, thus there's no best practice for implementing it. IIRC, GoF notes this as well. You might want to take a look at Strategy. –  Steve Evers Apr 7 '11 at 15:12

2 Answers 2

up vote 1 down vote accepted

I dont think the delegation pattern concerns its self with this. Delegation is about behaviour, what you appear to be talking about is ownership.

The ownership question really depends on your domain. Your object may have different life times, and as such may be able to legitimately maintain ownership for you, or alternatively they may have a short lifetime and must return ownership to the caller.

share|improve this answer

I think it is better to have the delegate create the obect to fill and return.

This allows for polymorphism in the solution where the caller does not have knowledge about the actual implementation details other than that the object created implements the interface that is part of the contract.

It is up to the delegate (or maybe a factory used by the delegate) to decide on the actual implementation class to use.

It all depends on context however. If your problem space consists of just 1 type of dictionary used throughout the system, the caller could create and pass the object to be filled. This would make it more difficult for the delegate however to return a "no-data" result.

share|improve this answer

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.