Delegates: Difference between revisions

Content added Content deleted
No edit summary
m (Fixed lang tags.)
Line 16: Line 16:
=={{header|Ada}}==
=={{header|Ada}}==
All that is needed in order to implement this is a common base type. The delegator holds a pointer to an "untyped" object from the base class. Querying if the target implements the delegate interface is done using run-time type identification.
All that is needed in order to implement this is a common base type. The delegator holds a pointer to an "untyped" object from the base class. Querying if the target implements the delegate interface is done using run-time type identification.
<lang ada>
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;


procedure Delegation is
procedure Delegation is
Line 64: Line 63:
A.Delegate := Has_Thing'Access; -- Set a thing
A.Delegate := Has_Thing'Access; -- Set a thing
Put_Line (A.Operation);
Put_Line (A.Operation);
end Delegation;
end Delegation;</lang>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 114: Line 112:
This example uses tango for output.
This example uses tango for output.


<lang D>
<lang D>import tango.io.Stdout;
import tango.io.Stdout;


class Delegator
class Delegator
Line 143: Line 140:
Stdout ( dr.setDg(thing).operation ).newline;
Stdout ( dr.setDg(thing).operation ).newline;
return 0;
return 0;
}</lang>
}

</lang>


=={{header|E}}==
=={{header|E}}==


def makeDelegator {
<lang e>def makeDelegator {
/** construct without an explicit delegate */
/** construct without an explicit delegate */
to run() {
to run() {
return makeDelegator(null)
return makeDelegator(null)
}
}

/** construct with a delegate */
/** construct with a delegate */
to run(delegateO) { # suffix because "delegate" is a reserved keyword
to run(delegateO) { # suffix because "delegate" is a reserved keyword
def delegator {
def delegator {
to operation() {
to operation() {
return if (delegateO.__respondsTo("thing", 0)) {
return if (delegateO.__respondsTo("thing", 0)) {
delegateO.thing()
delegateO.thing()
} else {
} else {
"default implementation"
"default implementation"
}
}
}
}
}
}
return delegator
return delegator
}
}
}
}

? def delegator := makeDelegator()
> delegator.operation()
# value: "default implementation"

? def delegator := makeDelegator(def doesNotImplement {})
> delegator.operation()
# value: "default implementation"


? def delegator := makeDelegator()
? def delegator := makeDelegator(def doesImplement {
> to thing() { return "delegate implementation" }
> delegator.operation()
> })
# value: "default implementation"
> delegator.operation()
# value: "default implementation"</lang>
? def delegator := makeDelegator(def doesNotImplement {})
> delegator.operation()
# value: "default implementation"
? def delegator := makeDelegator(def doesImplement {
> to thing() { return "delegate implementation" }
> })
> delegator.operation()
# value: "default implementation"


=={{header|Java}}==
=={{header|Java}}==
Line 327: Line 322:
=={{header|Oz}}==
=={{header|Oz}}==
{{trans|Python}}
{{trans|Python}}
<lang ocaml>functor
<lang oz>functor
import
import
Open Application Module
Open Application Module
Line 463: Line 458:


=={{header|Pop11}}==
=={{header|Pop11}}==
<lang pop11>uses objectclass;
<pre>
uses objectclass;
define :class Delegator;
define :class Delegator;
slot delegate = false;
slot delegate = false;
Line 495: Line 489:
;;; delegating to a freshly created Delegate
;;; delegating to a freshly created Delegate
newDelegate() -> delegate(a);
newDelegate() -> delegate(a);
operation(a) =>
operation(a) =></lang>

</pre>


=={{header|Python}}==
=={{header|Python}}==
Line 620: Line 612:


To code the <code>operation</code> method without relying on catching an exception, but strictly by using introspection:
To code the <code>operation</code> method without relying on catching an exception, but strictly by using introspection:
<lang tcl> method operation {} {
<lang tcl>method operation {} {
if { [info exists delegate] &&
if { [info exists delegate] &&
[info object isa object $delegate] &&
[info object isa object $delegate] &&
"thing" in [info object methods $delegate -all]
"thing" in [info object methods $delegate -all]
} then {
} then {
set result [$delegate thing]
set result [$delegate thing]
} else {
} else {
set result "default implementation"
set result "default implementation"
}
}
}</lang>
}</lang>


=={{header|Vorpal}}==
=={{header|Vorpal}}==
Delegate objects can be an array of delegates or as a single delegate.
Delegate objects can be an array of delegates or as a single delegate.
<lang vorpal>
<lang vorpal>a = new()
a = new()
a.f = method(){
a.f = method(){
.x.print()
.x.print()
Line 657: Line 648:
d.delegate = a
d.delegate = a
d.x = 7
d.x = 7
d.f()
d.f()</lang>
</lang>


The resulting output:
The resulting output: