|
From: Ben S. <bs...@vr...> - 2002-06-17 22:25:06
|
On Mon, 17 Jun 2002, Brown Joshua L wrote:
>
> >What if you did an observer pattern? Your object that creates the 5 other
> >objects can register itself as listeners to those 5 objects. When each of
> >the 5 objects generates somethings that needs to be added to the queue it
> >could fire off an event to all listeners. In this case, the listener would
> >be the object with the queue which would handle the event and add the
> >object to the queue.
>
> Basically what I really need to do is have each of the 5 sub-objects own a
> handle to their parent class that get's created when they are instantiated -
> that way I can access the public methods of the parent class. Is that how
> the observer model works?
Umm ... yeah. Here's an example...
// Registers self with subject and waits for it to invoke objectCreated.
class Observer
: public ObjectCreationListener
{
public:
Observer()
{
Subject* sub = new Subject();
sub->addObjectCreationListener(this);
sub->doThatSexyThingYouDo();
}
void objectCreated(MyObject* obj)
{
// do something with obj
}
};
// Creates objects and notifies listeners when they are created.
class Subject
{
public:
void addObjectCreationListener(ObjectCreationListener* l)
{
listeners.push_back(l);
}
void myFunc()
{
// hey ... we need to create an object
MyObject* obj = new MyObject();
listeners::iterator itr = listeners.begin();
while (itr != listeners.end())
{
(*itr)->objectCreated(obj);
}
}
private:
std::vector<ObjectCreationListener*> listeners;
};
cheers,
-----
Ben Scott
Research Assistant VRAC
bs...@ia...
|