VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
10.3 - The Signaler Pattern

Table of Contents

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.

10.3.1 The Signaler Pattern Specification

The Signaler pattern has the following requirements and behavior:

[<< The Rooted Singleton Pattern] [Home] [Top of Page] [The Factory Pattern >>]


Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)