ReferencesA Deeper Look at Signals and Slots
Observer pattern
Wikipedia
Boost Signals
Qt
Sourcestd/signals.d
Signal(T1...)import std.signals;int observedMessageCounter = 0;class Observer{// our slotvoid watch(string msg,int value) {switch (observedMessageCounter++) {case 0: writeln(msg);// "setting new value" writeln(value);// 4break;case 1: writeln(msg);// "setting new value" writeln(value);// 6break;default:assert(0,"Unknown observation"); } }}class Observer2{// our slotvoid watch(string msg,int value) { }}class Foo{int value() {return _value; }int value(int v) {if (v != _value) { _value = v;// call all the connected slots with the two parameters emit("setting new value", v); }return v; }// Mix in all the code we need to make Foo into a signalmixinSignal!(string,int);private :int _value;}Foo a =new Foo;Observer o =new Observer;auto o2 =new Observer2;auto o3 =new Observer2;auto o4 =new Observer2;auto o5 =new Observer2;a.value = 3;// should not call o.watch()a.connect(&o.watch);// o.watch is the slota.connect(&o2.watch);a.connect(&o3.watch);a.connect(&o4.watch);a.connect(&o5.watch);a.value = 4;// should call o.watch()a.disconnect(&o.watch);// o.watch is no longer a slota.disconnect(&o3.watch);a.disconnect(&o5.watch);a.disconnect(&o4.watch);a.disconnect(&o2.watch);a.value = 5;// so should not call o.watch()a.connect(&o2.watch);a.connect(&o.watch);// connect againa.value = 6;// should call o.watch()destroy(o);// destroying o should automatically disconnect ita.value = 7;// should not call o.watch()writeln(observedMessageCounter);// 2
slot_t = void delegate(T1);emit(T1i);connect(slot_tslot);disconnect(slot_tslot);disconnectAll();