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> |
||
⚫ | |||
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> |
|||
=={{header|E}}== |
=={{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 { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 327: | Line 322: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang |
<lang oz>functor |
||
import |
import |
||
Open Application Module |
Open Application Module |
||
Line 463: | Line 458: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
⚫ | |||
<pre> |
|||
⚫ | |||
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> |
<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}}== |
=={{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: |