2

We're studying the GOF design patterns and are blocked on the first two paragraphs of delegation. We cannot agree what the code being described in the shown paragraphs would look like. Delegation from GOF

Keith
  • 363
  • 5
  • 13
  • 2
    if my answer doesn't answer your question alredy, it would be helpful for us if you could explain where you guys have problems agreeing. Like what do *you* think the implementation would be and what does your friend think. – Gordon Dec 07 '17 at 10:02
  • This is the point about patterns: they don't describe codes. They describe strategies which you can implement in different ways/languages. It is better if you have a specific question about your implementation instead of asking about a code. – Duloren Dec 07 '17 at 10:20

2 Answers2

3

I guess the main thing that makes this difficult to understand is this passage:

But with inheritance, an inherited operation can always refer to the receiving object through the this member variable in C++ and self in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver.

Quoting Wikipedia (emphasis mine):

In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context. With language-level support for delegation, this is done implicitly by having self in the delegate refer to the original (sending) object, not the delegate (receiving object).

Whether it's actually self or this or current or caller is language specific. It means that in languages supporting delegation, the following would work:

Window
  int width: 640 
  int height: 480
  Rectangle rectangle: new Rectangle()
  int Area(): rectangle.Area()

Rectangle
  int Area(): self.width * self.height

Window window: new Window()
print window.Area()

This would print the result of 640x480 because the delegate executes Area within the context of the Window instance, although the code is in Rectangle. That is, it uses width and height from the Window through self.

In languages not supporting this automatic context passing, you would need to pass the caller to the delegate instead:

Window
  int width: 640 
  int height: 480
  Rectangle rectangle: new Rectangle()
  int Area(): rectangle.Area(this)

Rectangle
  int Area(context): context.width * context.height

Window window: new Window()
print window.Area()

When calling Area() we are passing the Window instance to the delegate (via this). And the delegate then accesses the width and height members to do it's computation through the explicitly passed argument.

Note that the pseudo language above makes no assumptions about things like types or visibility and we assume we can simply pass around the Windows instance like this and access it's members. Depending on your language uses, your mileage may vary.

So Delegation is really about binding context and not about mere forwarding of method calls. Quoting the Wikipedia page one last time:

Note that "delegation" is often used loosely to refer to the distinct concept of forwarding, where the sending object simply uses the corresponding member on the receiving object, evaluated in the context of the receiving object, not the original object.

Gordon
  • 312,688
  • 75
  • 539
  • 559
0

As far as the pattern is concerned and what the code looks like, the class diagram shows it pretty well: two classes, Window and Rectangle.

It's perhaps worth you get the actual book (if you don't have it yet in paper) and read one of the earlier chapters that describe the diagrams. Or maybe I'm wrong and it's not in that book but then get an additional other OOAD/OOP book that explains what these diagrams are about or how to work with them.

In case this seems silly so far: If it's worth, and I think this is coined by GoF as well, this is about to "favor aggregation over inheritance" which in itself is worth to resonate about. If yours all "we"s haven't yet, why not do that? Take the differently written codes/concepts to have some concrete examples to discuss about.

See as well:

hakre
  • 193,403
  • 52
  • 435
  • 836