When is a Delegate not a Delegate......

Ok, I just want to get this cleared up in my very confused mind....

I have always used the term 'delegate' when I have a class which implements an interface, but passes thru all requests to another object implementing the interface (e.g, simulating multiple inheritence, changing implementation at runtime etc etc).

I dont confuse it with Proxy or Decorator.

This pattern is described both in 'Design Patterns' (Gamma Et al) and 'Effective Java' (Joshua Bloch).

However, in 'Effective Java', Bloch says this is called 'forwarding' and says it is 'delegate'only if the wrapper object passes a reference to itself to the wrapped object.

I.e, if the wrapper doesn't pass itself to the wrapped object, it isn't delegate.

He goes as far as to say it is erroneous to call the pattern delgate if this doesn't hold (p75)

The justification for this is that in 'Design Patterns' (p20), an analogy is given to inheritence - where subclasses can defer requests to parent classes. It is stated that "to achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver".

That to me comes across as "if you want to have this effect with delegation, this is how you do it", not as "to be delegation, this must happen".

I wonder if the statement in Bloch (p75) is thus incorrect?

The more I think about this, the more Im convinced.... 'Design Patterns' goes on to state "Several design patterns use delegation. The State, Strategy and Visitor patterns depend on it".

Now, most of those certainly donot mandate that the wrapper reference is passed to the wrapped object - its usually anoption if its required (eg, State, Strategy).

I know this may seem like a pointless argument - but names really are very important when expressing patterns.

Its very important to me when describing patterns to others that I use the correct names so that everyone is clear on what we are talking about.....

Can anyone clear this distinction up?

[2117 byte] By [markeyMarka] at [2007-9-29 6:19:28]
# 1

Hi,

I am new to 'design patterns' and am just learning things, I have seen the book `design patterns` by Gamma. I found that he has demonstrated patterns using 'smalltalk'. My question to you is, how do you correlate the understanding of patterns based on two different languages.

I am now wondering are these design patterns `language neutral`!?

thanks!

oopsjunka at 2007-7-14 20:25:58 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Dukes on this one now(If anyone actually cares.........)
markeyMarka at 2007-7-14 20:25:58 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> Hi,

>

> I am new to 'design patterns' and am just learning

> things, I have seen the book `design patterns` by

> Gamma. I found that he has demonstrated patterns using

> 'smalltalk'. My question to you is, how do you

> correlate the understanding of patterns based on two

> different languages.

>

> I am now wondering are these design patterns `language

> neutral`!?

>

> thanks!

>

a design pattern is solution for a common problem found in software development. the implementation is not as important as the concept and can be used by any object oriented language such as small talk, c++, eiffel or java.

tpinarda at 2007-7-14 20:25:58 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Hi,

Rereading the quoted section of "Effective Java" I too can't figure out exactly what Bloch is meaning when he says:

"Technically, it's not delegation unless the wrapper object passes itself to the wrapped object".

To me, this means that the wrapped object must be able to accept the "wrapper" as an argument in either a constructor or a method (or, horror of horrors, by setting a field!). The wrapper object cannot pass itself to the wrapped object otherwise. This implies that classes must be specifically written to be used for delegation. Which doesn't really make sense to me at all.

I agree with you. I think Bloch is overstating it a bit. As far as I'm concerned it can still be treated as delegation if the wrapper object doesn't pass itself to the wrapped object. Though it might be simpler to just call it "composition and forwarding" and side-step the issue altogether.

O.

0sa at 2007-7-14 20:25:58 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Most of the Gamma patterns have the examples in C++ not smalltalk. In fact of the 4 guys that wrote that book ony 1 comes from Smalltalk and 3 from C++.Besides that's just the examples, the solution description is languaje neutral.
fedetxfa at 2007-7-14 20:25:58 > top of Java-index,Other Topics,Patterns & OO Design...