Jump to content

Delegates: Difference between revisions

m
Fixed lang tags.
No edit summary
m (Fixed lang tags.)
Line 16:
=={{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.
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Delegation is
Line 64 ⟶ 63:
A.Delegate := Has_Thing'Access; -- Set a thing
Put_Line (A.Operation);
end Delegation;</lang>
</lang>
Sample output:
<pre>
Line 114 ⟶ 112:
This example uses tango for output.
 
<lang D>import tango.io.Stdout;
import tango.io.Stdout;
 
class Delegator
Line 143 ⟶ 140:
Stdout ( dr.setDg(thing).operation ).newline;
return 0;
}</lang>
}
 
</lang>
 
=={{header|E}}==
 
<lang e>def makeDelegator {
/** construct without an explicit delegate */
to run() {
return makeDelegator(null)
}
 
/** construct with a delegate */
to run(delegateO) { # suffix because "delegate" is a reserved keyword
def delegator {
to operation() {
return if (delegateO.__respondsTo("thing", 0)) {
delegateO.thing()
} else {
"default implementation"
}
}
}
return delegator
}
}
 
? def delegator := makeDelegator(def doesImplement {)
> delegator.operation()
# value: "default implementation"
 
? 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"
> 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}}==
Line 327 ⟶ 322:
=={{header|Oz}}==
{{trans|Python}}
<lang ocamloz>functor
import
Open Application Module
Line 463 ⟶ 458:
 
=={{header|Pop11}}==
<lang pop11>uses objectclass;
<pre>
uses objectclass;
define :class Delegator;
slot delegate = false;
Line 495 ⟶ 489:
;;; delegating to a freshly created Delegate
newDelegate() -> delegate(a);
operation(a) =></lang>
 
</pre>
 
=={{header|Python}}==
Line 620 ⟶ 612:
 
To code the <code>operation</code> method without relying on catching an exception, but strictly by using introspection:
<lang tcl> method operation {} {
if { [info exists delegate] &&
[info object isa object $delegate] &&
"thing" in [info object methods $delegate -all]
} then {
set result [$delegate thing]
} else {
set result "default implementation"
}
}</lang>
 
=={{header|Vorpal}}==
Delegate objects can be an array of delegates or as a single delegate.
<lang vorpal>a = new()
a = new()
a.f = method(){
.x.print()
Line 657 ⟶ 648:
d.delegate = a
d.x = 7
d.f()</lang>
</lang>
 
The resulting output:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.