Delegates: Difference between revisions
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;
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;
class Delegator
Line 143 ⟶ 140:
Stdout ( dr.setDg(thing).operation ).newline;
return 0;
▲}</lang>
=={{header|E}}==
▲ > 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"
=={{header|Java}}==
Line 327 ⟶ 322:
=={{header|Oz}}==
{{trans|Python}}
<lang
import
Open Application Module
Line 463 ⟶ 458:
=={{header|Pop11}}==
<lang pop11>uses objectclass;▼
▲uses objectclass;
define :class Delegator;
slot delegate = false;
Line 495 ⟶ 489:
;;; delegating to a freshly created Delegate
newDelegate() -> delegate(a);
operation(a) =></lang>
=={{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>
=={{header|Vorpal}}==
Delegate objects can be an array of delegates or as a single delegate.
<lang vorpal>a = new()
a.f = method(){
.x.print()
Line 657 ⟶ 648:
d.delegate = a
d.x = 7
d.f()</lang>
The resulting output:
|
Revision as of 22:47, 19 November 2009
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A delegate is a helper object used by another object. The delegator may send the delegate certain messages, and provide a default implementation when there is no delegate or the delegate does not respond to a message. This pattern is heavily used in Cocoa framework on Mac OS X. See also wp:Delegation pattern.
Objects responsibilities:
Delegator:
- Keep an optional delegate instance.
- Implement "operation" method, returning the delegate "thing" if the delegate respond to "thing", or the string "default implementation".
Delegate:
- Implement "thing" and return the string "delegate implementation"
Show how objects are created and used. First, without a delegate, then with a delegate that does not implement "thing", and last with a delegate that implements "thing".
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;
procedure Delegation is
package Things is -- We need a common root for our stuff type Object is tagged null record; type Object_Ptr is access all Object'Class; -- Objects that have operation thing type Substantial is new Object with null record; function Thing (X : Substantial) return String; -- Delegator objects type Delegator is new Object with record Delegate : Object_Ptr; end record; function Operation (X : Delegator) return String; No_Thing : aliased Object; -- Does not have thing Has_Thing : aliased Substantial; -- Has one end Things; package body Things is function Thing (X : Substantial) return String is begin return "delegate implementation"; end Thing; function Operation (X : Delegator) return String is begin if X.Delegate /= null and then X.Delegate.all in Substantial'Class then return Thing (Substantial'Class (X.Delegate.all)); else return "default implementation"; end if; end Operation; end Things;
use Things;
A : Delegator; -- Without a delegate
begin
Put_Line (A.Operation); A.Delegate := No_Thing'Access; -- Set no thing Put_Line (A.Operation); A.Delegate := Has_Thing'Access; -- Set a thing Put_Line (A.Operation);
end Delegation;</lang> Sample output:
default implementation default implementation delegate implementation
Common Lisp
In CLOS, methods exist apart from classes, and are specialized based on the types of their arguments. This example defines two classes (delegator and delegate), and a thing generic method which is specialized in three ways: (1) for 'any' argument, providing a default method; (2) for delegators, where thing is recursively applied to the delegator's delegate (if there is one); and (3) for delegates.
<lang lisp>(defgeneric thing (object)
(:documentation "Thing the object."))
(defmethod thing (object)
"default implementation")
(defclass delegator ()
((delegate :initarg :delegate :reader delegator-delegate)))
(defmethod thing ((delegator delegator))
"If delegator has a delegate, invoke thing on the delegate,
otherwise return \"no delegate\"."
(if (slot-boundp delegator 'delegate) (thing (delegator-delegate delegator)) "no delegate"))
(defclass delegate () ())
(defmethod thing ((delegate delegate))
"delegate implementation")
(let ((d1 (make-instance 'delegator))
(d2 (make-instance 'delegator :delegate nil)) (d3 (make-instance 'delegator :delegate (make-instance 'delegate)))) (assert (string= "no delegate" (thing d1))) (assert (string= "default implementation" (thing d2))) (assert (string= "delegate implementation" (thing d3))))</lang>
D
D has built-in delegates, so we can skip createing additional Delegate object and pass real delegate directly to Delegator.
This example uses tango for output.
<lang D>import tango.io.Stdout;
class Delegator {
private char[] delegate() hasDelegate;
public:
char[] operation() { if (hasDelegate is null) return "default implementation"; return hasDelegate(); }
typeof(this) setDg(char[] delegate() dg) { hasDelegate = dg; return this; }
}
int main(char[][] args) {
auto dr = new Delegator(); auto thing = delegate char[]() { return "delegate implementation"; };
Stdout ( dr.operation ).newline; Stdout ( dr.operation ).newline; Stdout ( dr.setDg(thing).operation ).newline; return 0;
}</lang>
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() > 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"</lang>
Java
This implementation uses an interface called Thingable to specify the type of delegates that respond to thing(). The downside is that any delegate you want to use has to explicitly declare to implement the interface. The upside is that the type system guarantees that whent the delegate is non-null, it must implement the "thing" method.
<lang java>interface Thingable {
String thing();
}
class Delegator {
public Thingable delegate;
public String operation() { if (delegate == null) return "default implementation"; else return delegate.thing(); }
}
class Delegate implements Thingable {
public String thing() { return "delegate implementation"; }
}
// Example usage // Memory management ignored for simplification public class DelegateExample {
public static void main(String[] args) { // Without a delegate: Delegator a = new Delegator(); assert a.operation().equals("default implementation");
// With a delegate: Delegate d = new Delegate(); a.delegate = d; assert a.operation().equals("delegate implementation");
// Same as the above, but with an anonymous class: a.delegate = new Thingable() { public String thing() { return "anonymous delegate implementation"; } }; assert a.operation().equals("anonymous delegate implementation"); }
}</lang>
JavaScript
<lang 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") ;
}</lang>
Objective-C
<lang objc>#import <Cocoa/Cocoa.h>
@interface Delegator : NSObject {
id delegate;
} - (id)delegate; - (void)setDelegate:(id)obj; - (NSString *)operation; @end
@implementation Delegator - (id)delegate; {
return delegate;
} - (void)setDelegate:(id)obj; {
delegate = obj; // Weak reference
} - (NSString *)operation; {
if ([delegate respondsToSelector:@selector(thing)]) return [delegate thing]; return @"default implementation";
} @end
// Any object may implement these @interface NSObject (DelegatorDelegating) - (NSString *)thing; @end
@interface Delegate : NSObject // Don't need to declare -thing because any NSObject has this method @end
@implementation Delegate - (NSString *)thing; {
return @"delegate implementation";
} @end
// Example usage // Memory management ignored for simplification int main() {
// Without a delegate: Delegator *a = [[Delegator alloc] init]; NSLog(@"%d\n", [[a operation] isEqualToString:@"default implementation"]);
// With a delegate that does not implement thing: [a setDelegate:@"A delegate may be any object"]; NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
// With a delegate that implements "thing": Delegate *d = [[Delegate alloc] init]; [a setDelegate:d]; NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
return 0;
}</lang>
Oz
<lang oz>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</lang>
Perl
<lang perl>use strict;
package Delegator; sub new {
bless {}
} sub operation {
my ($self) = @_; if (defined $self->{delegate} && $self->{delegate}->can('thing')) { $self->{delegate}->thing; } else { 'default implementation'; }
} 1;
package Delegate; sub new {
bless {};
} sub thing {
'delegate implementation'
} 1;
package main;
- No delegate
my $a = Delegator->new; $a->operation eq 'default implementation' or die;
- With a delegate that does not implement "thing"
$a->{delegate} = 'A delegate may be any object'; $a->operation eq 'default implementation' or die;
- With delegate that implements "thing"
$a->{delegate} = Delegate->new; $a->operation eq 'delegate implementation' or die;</lang>
PHP
<lang 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->delegate = 'A delegate may be any object' ; print "{$a->operation()}\n" ;
$a->delegate = new Delegate() ; print "{$a->operation()}\n" ;</lang>
Pop11
<lang pop11>uses objectclass; define :class Delegator;
slot delegate = false;
enddefine;
define :class Delegate; enddefine;
define :method thing(x : Delegate);
'delegate implementation'
enddefine;
define :method operation(x : Delegator); if delegate(x) and fail_safe(delegate(x), thing) then
;;; Return value is on the stack
else
'default implementation'
endif; enddefine;
- Default, without a delegate
lvars a = newDelegator(); operation(a) =>
- a delegating to itself (works because Delegator does not
- implement thing)
a -> delegate(a); operation(a) =>
- delegating to a freshly created Delegate
newDelegate() -> delegate(a); operation(a) =></lang>
Python
<lang python>class Delegator:
def __init__(self): self.delegate = None def operation(self): if hasattr(self.delegate, 'thing') and callable(self.delegate.thing): return self.delegate.thing() return 'default implementation'
class Delegate:
def thing(self): return 'delegate implementation'
if __name__ == '__main__':
# No delegate a = Delegator() assert a.operation() == 'default implementation'
# With a delegate that does not implement "thing" a.delegate = 'A delegate may be any object' assert a.operation() == 'default implementation'
# With delegate that implements "thing" a.delegate = Delegate() assert a.operation() == 'delegate implementation'</lang>
Ruby
<lang ruby>class Delegator
attr_accessor :delegate def operation if @delegate.respond_to?(:thing) @delegate.thing else 'default implementation' end end
end
class Delegate
def thing 'delegate implementation' end
end
if __FILE__ == $PROGRAM_NAME
# No delegate a = Delegator.new puts a.operation # prints "default implementation"
# With a delegate that does not implement "thing" a.delegate = 'A delegate may be any object' puts a.operation # prints "default implementation"
# With delegate that implements "thing" a.delegate = Delegate.new puts a.operation # prints "delegate implementation"
end</lang>
Tcl
and the TclOO package
Uses Assertions#Tcl <lang tcl>package require Tcl 8.6
oo::class create Delegate {
method thing {} { return "delegate impl." } export thing
}
oo::class create Delegator {
variable delegate constructor args { my delegate {*}$args } method delegate args { if {[llength $args] == 0} { if {[info exists delegate]} { return $delegate } } elseif {[llength $args] == 1} { set delegate [lindex $args 0] } else { return -code error "wrong # args: should be \"[self] delegate ?target?\"" } } method operation {} { try { set result [$delegate thing] } on error e { set result "default implementation" } return $result }
}
- to instantiate a named object, use: class create objname; objname aMethod
- to have the class name the object: set obj [class new]; $obj aMethod
Delegator create a set b [Delegator new "not a delegate object"] set c [Delegator new [Delegate new]]
assert {[a operation] eq "default implementation"} ;# a "named" object, hence "a ..." assert {[$b operation] eq "default implementation"} ;# an "anonymous" object, hence "$b ..." assert {[$c operation] ne "default implementation"}
- now, set a delegate for object a
a delegate [$c delegate] assert {[a operation] ne "default implementation"}
puts "all assertions passed"</lang>
To code the operation
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>
Vorpal
Delegate objects can be an array of delegates or as a single delegate. <lang vorpal>a = new() a.f = method(){
.x.print()
}
c = new() c.g = method(){
(.x + 1).print()
}
- array of delegates
b = new() b.delegate = new() b.delegate[0] = a b.delegate[1] = c b.x = 3 b.f() b.g()
- single delegate
d = new() d.delegate = a d.x = 7 d.f()</lang>
The resulting output:
3 4 7