Delegates: Difference between revisions
Content added Content deleted
(Ada solution added) |
m (added php javascript oz) |
||
Line 72: | Line 72: | ||
delegate implementation |
delegate implementation |
||
</pre> |
</pre> |
||
=={{header|JavaScript}}== |
|||
{{trans|Python}} |
|||
<javascript>function Delegator() { |
|||
this.delegate = null ; |
|||
this.operation = function(){ |
|||
if(this.delegate && typeof(this.delegate.thing) == 'function') |
|||
return this.delegate.thing() ; |
|||
return 'default implementation' ; |
|||
} |
|||
} |
|||
function Delegate() { |
|||
this.thing = function(){ |
|||
return 'Delegate Implementation' ; |
|||
} |
|||
} |
|||
function testDelegator(){ |
|||
var a = new Delegator() ; |
|||
document.write(a.operation() + "\n") ; |
|||
a.delegate = 'A delegate may be any object' ; |
|||
document.write(a.operation() + "\n") ; |
|||
a.delegate = new Delegate() ; |
|||
document.write(a.operation() + "\n") ; |
|||
}</javascript> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
<pre> |
<pre> |
||
Line 136: | Line 163: | ||
} |
} |
||
</pre> |
</pre> |
||
=={{header|Oz}}== |
|||
{{trans|Python}} |
|||
<ocaml>functor |
|||
import |
|||
Open Application Module |
|||
define |
|||
Stdout = {New Open.file init(name:stdout)} |
|||
%% modify from http://osdir.com/ml/lang.mozart.user/2007-04/msg00042.html |
|||
%% not known if this is a right way ... |
|||
fun{IsMethod Klax Meth NArg} |
|||
B = {NewCell false} |
|||
in |
|||
if {IsObject Klax} then |
|||
[BootObj BootName] = {Module.link ['x-oz://boot/Object' 'x-oz://boot/Name']} |
|||
MethList = {BootObj.getClass Klax}.{BootName.newUnique 'ooMeth'} |
|||
MethName = {Dictionary.keys MethList} |
|||
in |
|||
{For 1 {Length MethName} 1 |
|||
proc{$ Idx} |
|||
if {Value.type MethName.Idx} == 'tuple' andthen |
|||
MethName.Idx.1 == Meth andthen |
|||
{Value.type {Dictionary.get MethList MethName.Idx.1}} == 'procedure' andthen |
|||
{ProcedureArity {Dictionary.get MethList MethName.Idx.1}} == NArg |
|||
then |
|||
B := true |
|||
end |
|||
end |
|||
} |
|||
end |
|||
@B |
|||
end |
|||
%% translate from Python version |
|||
class Delegator |
|||
attr delegate |
|||
meth operation(?R) |
|||
if {IsMethod @delegate 'thing' 1} then |
|||
{@delegate thing(R)} |
|||
else |
|||
R = 'default implementation' |
|||
end |
|||
end |
|||
meth init |
|||
delegate := {New BaseObject noop} |
|||
end |
|||
meth set(DG) |
|||
delegate := DG |
|||
end |
|||
end |
|||
class Delegate from BaseObject |
|||
meth thing(?R) |
|||
R = 'Delegate Implementation' |
|||
end |
|||
end |
|||
A = {New Delegator init} |
|||
{Stdout write(vs:{A operation($)}#'\n')} |
|||
{A set('A delegate may be any object')} |
|||
{Stdout write(vs:{A operation($)}#'\n')} |
|||
{A set({New Delegate noop})} |
|||
{Stdout write(vs:{A operation($)}#'\n')} |
|||
{Application.exit 0} |
|||
end</ocaml> |
|||
=={{header|PHP}}== |
|||
{{trans|Python}} |
|||
<php>class Delegator { |
|||
function __construct() { |
|||
$this->delegate = NULL ; |
|||
} |
|||
function operation() { |
|||
if(method_exists($this->delegate, "thing")) |
|||
return $this->delegate->thing() ; |
|||
return 'default implementation' ; |
|||
} |
|||
} |
|||
class Delegate { |
|||
function thing() { |
|||
return 'Delegate Implementation' ; |
|||
} |
|||
} |
|||
$a = new Delegator() ; |
|||
print "{$a->operation()}\n" ; |
|||
$a->deleagte = 'A delegate may be any object' ; |
|||
print "{$a->operation()}\n" ; |
|||
$a->delegate = new Delegate() ; |
|||
print "{$a->operation()}\n" ;</php> |
|||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
<pre> |
<pre> |