![]() |
VR-Forces 4.0.4 Class Documentation
|
The Signaler pattern lets signals live outside the object about which they are signaling.
Sometimes signals for objects are hard to use. For example, if FirstObject wants to connect to a signal emitted by SecondObject, but SecondObject does not exist yet, creating the connection is a problem. We have created the Signaler pattern to solve this problem. Now, FirstObject connects to signals emitted by SecondObjectSignaler, a Rooted Singleton. Then SecondObject has the SecondObjectSignal emit the signals causing FirstObject to get signalled.
Here is a simple example:
// Class that wants to let others know about a change in speed. class Foo { public: void setSpeed(double s) { mySpeed = s; FooSignaler::instance( myDisplayEngine).signal_speedChanged(this,mySpeed); } protected: DtDE& myDisplayEngine; }; // Class that emits signal_speedChanged; class FooSignaler { public: // Lazily create a global instance rooted off a Display Engine static FooSignaler& instance(DtDE displayEngine) { FooSignaler* singleton = dynamic_cast<FooSignaler*>( displayEngine.findInstance("FooSignaler")); if(!singleton) { singleton = new FooSignaler(); displayEngine.registerInstance("FooSignaler",singleton); } return *singleton; } // Signal to emit. signal < void (Foo*, double) > signal_speedChanged; };
Now, an interested party can register with the FooSignaler before an instance of the Foo object actually exists:
FooSignaler::instance(myDisplayEngine).signal_speedChanged.connect( boost::bind(&InterestedPartyClass::onSpeedChanged, &insterestedPartyClassInstance, _1)));
The signaler is lazily created and the InterestedPartyClass gets signaled whenever an instance of Foo comes into existence and setSpeed() is called on it.
The Signaler pattern has the following requirements and behavior:
[<< The Rooted Singleton Pattern] [Home] [Top of Page] [The Factory Pattern >>]