/* * An informal interface for delegates to Something */ interface SomethingDelegate { 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 : SomethingDelegate { 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(); }