CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
79
Task/Delegates/C++/delegates.cpp
Normal file
79
Task/Delegates/C++/delegates.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#include <tr1/memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <tr1/functional>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::tr1;
|
||||
using std::tr1::function;
|
||||
|
||||
// interface for all delegates
|
||||
class IDelegate
|
||||
{
|
||||
public:
|
||||
virtual ~IDelegate() {}
|
||||
};
|
||||
|
||||
//interface for delegates supporting thing
|
||||
class IThing
|
||||
{
|
||||
public:
|
||||
virtual ~IThing() {}
|
||||
virtual std::string Thing() = 0;
|
||||
};
|
||||
|
||||
// Does not handle Thing
|
||||
class DelegateA : virtual public IDelegate
|
||||
{
|
||||
};
|
||||
|
||||
// Handles Thing
|
||||
class DelegateB : public IThing, public IDelegate
|
||||
{
|
||||
std::string Thing()
|
||||
{
|
||||
return "delegate implementation";
|
||||
}
|
||||
};
|
||||
|
||||
class Delegator
|
||||
{
|
||||
public:
|
||||
std::string Operation()
|
||||
{
|
||||
if(Delegate) //have delegate
|
||||
if (IThing * pThing = dynamic_cast<IThing*>(Delegate.get()))
|
||||
//delegate provides IThing interface
|
||||
return pThing->Thing();
|
||||
|
||||
return "default implementation";
|
||||
}
|
||||
|
||||
shared_ptr<IDelegate> Delegate;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
shared_ptr<DelegateA> delegateA(new DelegateA());
|
||||
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;
|
||||
|
||||
/*
|
||||
Prints:
|
||||
|
||||
default implementation
|
||||
default implementation
|
||||
delegate implementation
|
||||
*/
|
||||
}
|
||||
39
Task/Delegates/C-sharp/delegates.cs
Normal file
39
Task/Delegates/C-sharp/delegates.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
|
||||
interface IOperable
|
||||
{
|
||||
string Operate();
|
||||
}
|
||||
|
||||
class Inoperable
|
||||
{
|
||||
}
|
||||
|
||||
class Operable : IOperable
|
||||
{
|
||||
public string Operate()
|
||||
{
|
||||
return "Delegate implementation.";
|
||||
}
|
||||
}
|
||||
|
||||
class Delegator : IOperable
|
||||
{
|
||||
object Delegate;
|
||||
|
||||
public string Operate()
|
||||
{
|
||||
var operable = Delegate as IOperable;
|
||||
return operable != null ? operable.Operate() : "Default implementation.";
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
var delegator = new Delegator();
|
||||
foreach (var @delegate in new object[] { null, new Inoperable(), new Operable() })
|
||||
{
|
||||
delegator.Delegate = @delegate;
|
||||
Console.WriteLine(delegator.Operate());
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Task/Delegates/Common-Lisp/delegates.lisp
Normal file
29
Task/Delegates/Common-Lisp/delegates.lisp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(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))))
|
||||
24
Task/Delegates/D/delegates-1.d
Normal file
24
Task/Delegates/D/delegates-1.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Delegator {
|
||||
string delegate() hasDelegate;
|
||||
|
||||
string operation() {
|
||||
if (hasDelegate is null)
|
||||
return "Default implementation";
|
||||
return hasDelegate();
|
||||
}
|
||||
|
||||
typeof(this) setDg(string delegate() dg) {
|
||||
hasDelegate = dg;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
auto dr = new Delegator;
|
||||
string delegate() thing = () => "Delegate implementation";
|
||||
|
||||
writeln(dr.operation());
|
||||
writeln(dr.operation());
|
||||
writeln(dr.setDg(thing).operation());
|
||||
}
|
||||
29
Task/Delegates/D/delegates-2.d
Normal file
29
Task/Delegates/D/delegates-2.d
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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;
|
||||
}
|
||||
29
Task/Delegates/Dart/delegates.dart
Normal file
29
Task/Delegates/Dart/delegates.dart
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
class Delegator {
|
||||
var delegate;
|
||||
|
||||
String operation() {
|
||||
if (delegate == null)
|
||||
return "default implementation";
|
||||
else
|
||||
return delegate.thing();
|
||||
}
|
||||
}
|
||||
|
||||
class Delegate {
|
||||
String thing() => "delegate implementation";
|
||||
}
|
||||
|
||||
main() {
|
||||
// Without a delegate:
|
||||
Delegator a = new Delegator();
|
||||
Expect.equals("default implementation",a.operation());
|
||||
|
||||
// any object doesn't work unless we can check for existing methods
|
||||
// a.delegate=new Object();
|
||||
// Expect.equals("default implementation",a.operation());
|
||||
|
||||
// With a delegate:
|
||||
Delegate d = new Delegate();
|
||||
a.delegate = d;
|
||||
Expect.equals("delegate implementation",a.operation());
|
||||
}
|
||||
50
Task/Delegates/Delphi/delegates-1.delphi
Normal file
50
Task/Delegates/Delphi/delegates-1.delphi
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
unit Printer;
|
||||
|
||||
interface
|
||||
|
||||
type
|
||||
// the "delegate"
|
||||
TRealPrinter = class
|
||||
public
|
||||
procedure Print;
|
||||
end;
|
||||
|
||||
// the "delegator"
|
||||
TPrinter = class
|
||||
private
|
||||
FPrinter: TRealPrinter;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure Print;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TRealPrinter }
|
||||
|
||||
procedure TRealPrinter.Print;
|
||||
begin
|
||||
Writeln('Something...');
|
||||
end;
|
||||
|
||||
{ TPrinter }
|
||||
|
||||
constructor TPrinter.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
FPrinter:= TRealPrinter.Create;
|
||||
end;
|
||||
|
||||
destructor TPrinter.Destroy;
|
||||
begin
|
||||
FPrinter.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TPrinter.Print;
|
||||
begin
|
||||
FPrinter.Print;
|
||||
end;
|
||||
|
||||
end.
|
||||
19
Task/Delegates/Delphi/delegates-2.delphi
Normal file
19
Task/Delegates/Delphi/delegates-2.delphi
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
program Delegate;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils,
|
||||
Printer in 'Printer.pas';
|
||||
|
||||
var
|
||||
PrinterObj: TPrinter;
|
||||
begin
|
||||
PrinterObj:= TPrinter.Create;
|
||||
try
|
||||
PrinterObj.Print;
|
||||
Readln;
|
||||
finally
|
||||
PrinterObj.Free;
|
||||
end;
|
||||
end.
|
||||
34
Task/Delegates/E/delegates.e
Normal file
34
Task/Delegates/E/delegates.e
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue