Delegates: Difference between revisions
No edit summary |
No edit summary |
||
Line 70: | Line 70: | ||
delegate implementation |
delegate implementation |
||
</pre> |
</pre> |
||
=={{header|Aikido}}== |
|||
<lang aikido> |
|||
class Delegator { |
|||
public generic delegate = none |
|||
public function operation { |
|||
if (typeof(delegate) == "none") { |
|||
return "default implementation" |
|||
} |
|||
return delegate() |
|||
} |
|||
} |
|||
function thing { |
|||
return "delegate implementation" |
|||
} |
|||
// default, no delegate |
|||
var d = new Delegator() |
|||
println (d.operation()) |
|||
// delegate |
|||
var d1 = new Delegator() |
|||
d1.delegate = thing |
|||
println (d1.operation()) |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
Revision as of 06:28, 17 March 2010
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
Aikido
<lang aikido> class Delegator {
public generic delegate = none
public function operation { if (typeof(delegate) == "none") { return "default implementation" } return delegate() }
}
function thing {
return "delegate implementation"
}
// default, no delegate var d = new Delegator() println (d.operation())
// delegate var d1 = new Delegator() d1.delegate = thing println (d1.operation())
</lang>
C
As best you can do, without support for classes. <lang c>#include <stdio.h>
- include <stdlib.h>
- include <string.h>
typedef const char * (*Responder)( int p1);
typedef struct sDelegate {
Responder operation;
} *Delegate;
/* Delegate class constructor */ Delegate NewDelegate( Responder rspndr ) {
Delegate dl = (Delegate)malloc(sizeof(struct sDelegate)); dl->operation = rspndr; return dl;
}
/* Thing method of Delegate */ const char *DelegateThing(Delegate dl, int p1) {
return (dl->operation)? (*dl->operation)(p1) : NULL;
}
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ typedef struct sDelegator {
int param; char *phrase; Delegate delegate;
} *Delegator;
const char * defaultResponse( int p1) {
return "default implementation";
}
static struct sDelegate defaultDel = { &defaultResponse };
/* Delegator class constructor */ Delegator NewDelegator( int p, char *phrase) {
Delegator d = (Delegator)malloc(sizeof(struct sDelegator)); d->param = p; d->phrase = phrase; d->delegate = &defaultDel; /* default delegate */ return d;
}
/* Operation method of Delegator */ const char *Delegator_Operation( Delegator theDelegator, int p1, Delegate delroy) {
const char *rtn; if (delroy) { rtn = DelegateThing(delroy, p1); if (!rtn) { /* delegate didn't handle 'thing' */ rtn = DelegateThing(theDelegator->delegate, p1); } } else /* no delegate */ rtn = DelegateThing(theDelegator->delegate, p1);
printf("%s\n", theDelegator->phrase ); return rtn;
}
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ const char *thing1( int p1) {
printf("We're in thing1 with value %d\n" , p1); return "delegate implementation";
}
int main() {
Delegate del1 = NewDelegate(&thing1); Delegate del2 = NewDelegate(NULL); Delegator theDelegator = NewDelegator( 14, "A stellar vista, Baby.");
printf("Delegator returns %s\n\n", Delegator_Operation( theDelegator, 3, NULL)); printf("Delegator returns %s\n\n", Delegator_Operation( theDelegator, 3, del1)); printf("Delegator returns %s\n\n", Delegator_Operation( theDelegator, 3, del2)); return 0;
}</lang>
C++
<lang Cpp>#include <boost/shared_ptr.hpp>
- include <string>
- include <iostream>
// interface class IDelegate { public:
virtual std::string Thing() { return "default implementation"; }
protected:
IDelegate() {}
};
// Does not handle Thing class DelegateA : public IDelegate { };
// Handles Thing class DelegateB : public IDelegate {
std::string Thing() { return "delegate implementation"; }
};
class Delegator { public:
std::string Operation() { if(Delegate) return Delegate->Thing(); else return "no implementation"; }
boost::shared_ptr<IDelegate> Delegate;
};
int main() {
boost::shared_ptr<DelegateA> delegateA(new DelegateA()); boost::shared_ptr<DelegateB> delegateB(new DelegateB()); Delegator delegator;
// No delegate std::cout << delegator.Operation() << std::endl;
// Delegate doesn't handle "Thing" delegator.Delegate = delegateA; std::cout << delegator.Operation() << std::endl;
// Delegate handles "Thing" delegator.Delegate = delegateB; std::cout << delegator.Operation() << std::endl;
}</lang>
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>
J
Life becomes slightly cleaner if we delegate to ourselves in the absence of some other delegate.
<lang J>coclass 'delegator'
operation=:3 :'thing__delegate ::thing y' thing=: 'default implementation'"_ setDelegate=:3 :'delegate=:y' delegate=:<'delegator'
coclass 'delegatee1'
coclass 'delegatee2'
thing=: 'delegate implementation'"_
NB. set context for interactive use of this script: cocurrent 'base'</lang>
Example use:
<lang J> obj=:conew'delegator'
operation__obj
default implementation
setDelegate__obj conew'delegatee1'
┌─┐ │4│ └─┘
operation__obj
default implementation
setDelegate__obj conew'delegatee2'
┌─┐ │5│ └─┘
operation__obj
delegate 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>declare
class Delegator from BaseObject attr
delegate:unit
meth set(DG)
{Object.is DG} = true %% assert: DG must be an object delegate := DG
end
meth operation($)
if @delegate == unit then {self default($)} else try {@delegate thing($)} catch error(object(lookup ...) ...) then %% the delegate did not understand the message {self default($)} end end
end
meth default($)
"default implementation"
end end class Delegate from BaseObject meth thing($)
"delegate Implementation"
end end A = {New Delegator noop}
in
{System.showInfo {A operation($)}}
{A set({New BaseObject noop})} {System.showInfo {A operation($)}}
{A set({New Delegate noop})} {System.showInfo {A operation($)}}</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