Signals and Slots in D
Fredrik Olsson
peylow at gmail.com
Fri Sep 29 01:58:30 PDT 2006
Fredrik Olsson skrev:
> Walter Bright skrev:
>> Fredrik Olsson wrote:
<snip>
> So an "informal interface" is a interface of methods that could be
> implemented, not an interface of methods that must be implemented. The
> methods are virtual, so testing for implementation should be as easy as
> comparing for NULL in the VMT.
>
A more complete example of how "informal interfaces" could work in reality:
/*
* An informal interface for delegates to Something
* If not implemented shouldDoStuff returns true.
*/
interface SomethingDelagete {
optional bool shouldDoStuff(Somethin) = true;
optional void willDoStuff(Something);
optional void didDoStuff(Something);
}
/*
* The Something class.
* That will ask a delegate for permission, and in
* general keep it informed when doing stuff.
* If one is set that is.
*/
class Something {
SomethingDelegate delegate;
void doStuff() {
if (delegate.shouldDoStuff(this)) {
delegate.willDoStuff(this);
writefln("Doing stuff");
delegate.didDoStuff(this);
}
}
char[] toString() { return "Foo"; }
}
/*
* An actual delegate that keeps track of Somethings doing.
* But ignores telling it what to do.
*/
class MyDelegate {
void willDoStuff(Something s) {
writefln(s.toString ~ " will do stuff");
}
void didDoStuff(Something s) {
writefln(s.toString ~ " did do stuff");
}
}
/*
* This will output:
* Foo will do stuff
* Doing stuff
* Foo did do stuff
*/
void main() {
Something something = new Something();
something.delegate = new MyDelegate();
something.doStuff();
}
// Fredrik Olsson
More information about the Digitalmars-d
mailing list