Talk:Delegation (object-oriented programming)
JavaScript
JavaScript implements delegation, maybe that should be mentioned.
In JavaScript, to parse foo.bar, the compiler first searches the list of object foo's properties for one named "bar", then if not found the list of properties of object foo's prototype, then recursively to the Object built-in object prototype which has some identifiers shared by all objects, such as toString, a method that returns a string description of the object. If the string "bar" was not found, it returns the "undefined" object; else if the found property holds a primitive, it returns its value; if it is an object, it returns a reference to it. In JavaScript, a function is an object (which you can call). A method is an function in which the identifier "this" refers to the object it is a property of.
var A = {}; // same as A = new Object()
var B = ( function() {
var F = function () {};
F.prototype = A;
return new F();
}());
// from JavaScript 1.8.5 Object.create(A) returns same as above code
// now object A is in the lookup chain for object B's refinements
A.counter = 0;
A.separ = ": ";
B.separ = ", ";
for ( A.counter = 0 ; A.counter < 10 ; A.counter++) {
document.write(B.counter + B.separ); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
}
That's delegation, isn't it? I don't know where to place this example in the page. I don't quite get the structure. There's a mention of JavaScript and ECMAScript, but the main example refers to language that do not implement prototype-based inheritance.
In several places the article text refers to methods. Is this part of the definition of delegation? JavaScript/ECMAScript treats fields and methods alike. The above example could be written with a closure and methods:
var counter = (function () {
var n = 0;
return {
increment: function () {n++;},
get: function () {return n;}
};
}());
counter.separ = ": ";
var myObject = ( function() {
var F = function () {};
F.prototype = counter;
return new F();
}());
// from JavaScript 1.8.5 Object.create(Counter) returns same as above code
// now object Counter is in the lookup chain for object B's refinements
myObject.separ = ", ";
while (counter.get() < 10) {
document.write(myObject.get() + myObject.separ); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
myObject.increment(); // same as counter.increment()
}
I shouldn't say that makes things any clearer; but if the point is inheriting methods, that's it.
Polbrian (talk) 08:06, 24 September 2011 (UTC)
Additional cross reference
I don't see a link on this page to Observer pattern. I'm new to this stuff, but aren't these two topics intrinsically related? Kmote (talk) 22:10, 15 December 2008 (UTC)
Old definition
Shouldn't the old definition go under the modern one? Most people don't read more than one page, which would mean only the old definition is read. - September 26th 2005
- Good point. Wouter Lievens 10:20, 26 September 2005 (UTC)
Third Definition
The third definition, as given in [Taivalsaari, 1996] is missing. Shall I add it? Wouter Lievens 19:37, 1 May 2005 (UTC)
- Now that I think of it, the definition is equivalent. Wouter Lievens 14:35, 11 May 2005 (UTC)
Language Feature example wrong?
I don't get this. If a in class B were a usual field, this in A::foo() would refer to the a object, so you'd get "a.bar" printed. With the delegation language feature, it should be "b.bar", since this still refers to object b. Giese 09:37, 27 September 2006 (UTC)
- Giese, even though no-one has responded to your concern in nearly 3 years, I think you are right. When b.foo is called, (class A).bar should start lookup (Smalltalk style) at b.bar which is B.bar, and thus print("b.bar")
- I may be wrong, but my first impression is that the Java/C# example contradicts the text. If I understand correctly, the text insists that the method binding is done at run time, but the example appears to bind class B (the lookup of B.foo) to class A (looking up A.bar) at compile time. Have I misunderstood the Java/C# pseudocode? Could the article be expanded a little to explain how the program can delegate B.foo to different methods (such as C.foo and D.foo) depending on a run time delegation link, depending on the class/state of a when new B(a) is called) at run time? --Hroðulf (or Hrothulf) (Talk) 08:22, 29 June 2009 (UTC)
- I don't know anything about this, but I also think Giese is right. The page Consultation_(object-oriented_programming) says that one of the main differences between delegation and consultation is that delegation has "late binding of self". This term is explained in Fig. 1 of the paper "Intersecting Classes and Prototypes", and it seems like it is consistent with Giese's suggestion. The example in the article seems to be an example of consultation. Also, this article should make reference to and define "late binding of self". A start: In delegation, when the delegator object is called, the delegator calls the delegee code. Late binding of self means that if the delegee code refers to "self", then that refers to the delegator object, not to the delegatee object. Bayle Shanks (talk) 01:40, 11 December 2009 (UTC)
Hard to understand
This page needs some cleanup work to make it easier to read. I can't understand half of what its talking about. This should be readable to people who have never heard of a delegate. Fresheneesz 00:18, 22 November 2006 (UTC)
- Would make more sense if the first example were replcaed with the better one from Delegation pattern.
Merge with Delegation pattern
This article talks at length about the Delegation pattern, yet Delegation pattern already has its own node. Shouldn't they be merged? --Devnevyn 12:45, 11 August 2007 (UTC)
I second this. This page is not very clear to me even as one who already understands its subject. 76.187.248.215 (talk) 09:07, 15 July 2008 (UTC)
- Agreed. Would make more sense if the first example were replcaed with the better one from Delegation pattern too. — Preceding unsigned comment added by 122.148.41.172 (talk) 13:37, 18 November 2012 (UTC)
Merging with Delegate (.NET)
I am removing the merge proposal, as a delegate object as per .NET terminology is deeply different from the Delegation design pattern presented here. - 62.101.126.215 (talk) 16:08, 21 January 2008 (UTC)
"so-called self-calls"
Most commonly, [delegation] refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems
I can't find the term "self-call" on wikipeia or in the paper ( http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html ), and a paper reference rather than an explanation is pretty horrible. Perhaps a common example would help, (if I've understood this correctly at all) "..feature, such as Java's virtual method calls, making use of..". //22oct2011 — Preceding unsigned comment added by 65.31.112.33 (talk) 18:53, 22 October 2011 (UTC)
==definition== in (Wikipedia's) "Glossary of Unified Modeling Language terms" it is defined as a "Message from an Object to one of its own Methods". This could be edited into the link but I forgot how to do it. lifeform (talk) 06:44, 3 March 2014 (UTC)