new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
12
Task/Delegates/0DESCRIPTION
Normal file
12
Task/Delegates/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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 [http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/chapter_5_section_3.html#//apple_ref/doc/uid/TP40002974-CH6-DontLinkElementID_93 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".
|
||||
2
Task/Delegates/1META.yaml
Normal file
2
Task/Delegates/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Object oriented
|
||||
86
Task/Delegates/C/delegates.c
Normal file
86
Task/Delegates/C/delegates.c
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#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 = 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 = 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;
|
||||
}
|
||||
4
Task/Delegates/CoffeeScript/delegates-2.coffee
Normal file
4
Task/Delegates/CoffeeScript/delegates-2.coffee
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
> coffee foo.coffee
|
||||
default implementation
|
||||
default implementation
|
||||
Delegate Implementation
|
||||
24
Task/Delegates/CoffeeScript/delegates.coffee
Normal file
24
Task/Delegates/CoffeeScript/delegates.coffee
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Delegator
|
||||
operation: ->
|
||||
if @delegate and typeof (@delegate.thing) is "function"
|
||||
return @delegate.thing()
|
||||
"default implementation"
|
||||
|
||||
class Delegate
|
||||
thing: ->
|
||||
"Delegate Implementation"
|
||||
|
||||
testDelegator = ->
|
||||
# Delegator with no delegate.
|
||||
a = new Delegator()
|
||||
console.log a.operation()
|
||||
|
||||
# Delegator with delegate not implementing "thing"
|
||||
a.delegate = "A delegate may be any object"
|
||||
console.log a.operation()
|
||||
|
||||
# Delegator with delegate that does implement "thing"
|
||||
a.delegate = new Delegate()
|
||||
console.log a.operation()
|
||||
|
||||
testDelegator()
|
||||
39
Task/Delegates/Go/delegates.go
Normal file
39
Task/Delegates/Go/delegates.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
import "fmt"
|
||||
|
||||
type Delegator struct {
|
||||
delegate interface{} // the delegate may be any type
|
||||
}
|
||||
|
||||
// interface that represents anything that supports thing()
|
||||
type Thingable interface {
|
||||
thing() string
|
||||
}
|
||||
|
||||
func (self Delegator) operation() string {
|
||||
if v, ok := self.delegate.(Thingable); ok {
|
||||
return v.thing()
|
||||
}
|
||||
return "default implementation"
|
||||
}
|
||||
|
||||
type Delegate int // any dummy type
|
||||
|
||||
func (Delegate) thing() string {
|
||||
return "delegate implementation"
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Without a delegate:
|
||||
a := Delegator{}
|
||||
fmt.Println(a.operation()) // prints "default implementation"
|
||||
|
||||
// With a delegate that does not implement "thing"
|
||||
a.delegate = "A delegate may be any object"
|
||||
fmt.Println(a.operation()) // prints "default implementation"
|
||||
|
||||
// With a delegate:
|
||||
var d Delegate
|
||||
a.delegate = d
|
||||
fmt.Println(a.operation()) // prints "delegate implementation"
|
||||
}
|
||||
43
Task/Delegates/Java/delegates.java
Normal file
43
Task/Delegates/Java/delegates.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
25
Task/Delegates/JavaScript/delegates.js
Normal file
25
Task/Delegates/JavaScript/delegates.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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") ;
|
||||
}
|
||||
25
Task/Delegates/PHP/delegates.php
Normal file
25
Task/Delegates/PHP/delegates.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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" ;
|
||||
54
Task/Delegates/Perl/delegates-2.pl
Normal file
54
Task/Delegates/Perl/delegates-2.pl
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use 5.010_000;
|
||||
|
||||
package Delegate::Protocol
|
||||
use Moose::Role;
|
||||
# All methods in the Protocol is optional
|
||||
#optional 'thing';
|
||||
# If we wanted to have a required method, we would state:
|
||||
# requires 'required_method';
|
||||
#
|
||||
|
||||
package Delegate::NoThing;
|
||||
use Moose;
|
||||
with 'Delegate::Protocol';
|
||||
|
||||
package Delegate;
|
||||
use Moose;
|
||||
|
||||
# The we confirm to Delegate::Protocol
|
||||
with 'Delegate::Protocol';
|
||||
sub thing { 'delegate implementation' };
|
||||
|
||||
package Delegator;
|
||||
use Moose;
|
||||
|
||||
has delegate => (
|
||||
is => 'rw',
|
||||
does => 'Delegate::Protocol', # Moose insures that the delegate confirms to the protocol.
|
||||
predicate => 'hasDelegate'
|
||||
);
|
||||
|
||||
sub operation {
|
||||
|
||||
my ($self) = @_;
|
||||
if( $self->hasDelegate && $self->delegate->can('thing') ){
|
||||
return $self->delegate->thing() . $postfix; # we are know that delegate has thing.
|
||||
} else {
|
||||
return 'default implementation';
|
||||
}
|
||||
};
|
||||
|
||||
package main;
|
||||
use strict;
|
||||
|
||||
# No delegate
|
||||
my $delegator = Delegator->new();
|
||||
$delegator->operation eq 'default implementation' or die;
|
||||
|
||||
# With a delegate that does not implement "thing"
|
||||
$delegator->delegate( Delegate::NoThing->new );
|
||||
$delegator->operation eq 'default implementation' or die;
|
||||
|
||||
# With delegate that implements "thing"
|
||||
$delegator->delegate( Delegate->new );
|
||||
$delegator->operation eq 'delegate implementation' or die;
|
||||
38
Task/Delegates/Perl/delegates.pl
Normal file
38
Task/Delegates/Perl/delegates.pl
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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;
|
||||
30
Task/Delegates/PicoLisp/delegates.l
Normal file
30
Task/Delegates/PicoLisp/delegates.l
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
(class +Delegator)
|
||||
# delegate
|
||||
|
||||
(dm operation> ()
|
||||
(if (: delegate)
|
||||
(thing> @)
|
||||
"default implementation" ) )
|
||||
|
||||
|
||||
(class +Delegate)
|
||||
# thing
|
||||
|
||||
(dm T (Msg)
|
||||
(=: thing Msg) )
|
||||
|
||||
(dm thing> ()
|
||||
(: thing) )
|
||||
|
||||
|
||||
(let A (new '(+Delegator))
|
||||
# Without a delegate
|
||||
(println (operation> A))
|
||||
|
||||
# With delegate that does not implement 'thing>'
|
||||
(put A 'delegate (new '(+Delegate)))
|
||||
(println (operation> A))
|
||||
|
||||
# With delegate that implements 'thing>'
|
||||
(put A 'delegate (new '(+Delegate) "delegate implementation"))
|
||||
(println (operation> A)) )
|
||||
25
Task/Delegates/Python/delegates.py
Normal file
25
Task/Delegates/Python/delegates.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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'
|
||||
19
Task/Delegates/Ruby/delegates-2.rb
Normal file
19
Task/Delegates/Ruby/delegates-2.rb
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
require 'forwardable'
|
||||
|
||||
class Delegator; extend Forwardable
|
||||
attr_accessor :delegate
|
||||
def_delegator :@delegate, :thing, :delegated
|
||||
|
||||
def initialize
|
||||
@delegate = Delegate.new()
|
||||
end
|
||||
end
|
||||
|
||||
class Delegate
|
||||
def thing
|
||||
'Delegate'
|
||||
end
|
||||
end
|
||||
|
||||
a = Delegator.new
|
||||
puts a.delegated # prints "Delegate"
|
||||
31
Task/Delegates/Ruby/delegates.rb
Normal file
31
Task/Delegates/Ruby/delegates.rb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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
|
||||
10
Task/Delegates/Tcl/delegates-2.tcl
Normal file
10
Task/Delegates/Tcl/delegates-2.tcl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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"
|
||||
}
|
||||
}
|
||||
53
Task/Delegates/Tcl/delegates.tcl
Normal file
53
Task/Delegates/Tcl/delegates.tcl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package require TclOO
|
||||
|
||||
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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue