472,341 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 software developers and data experts.

How can you make different existing classes into "observers"?

Given two existing but different classes OldA and OldB (that can not be
made to derive from any new base class); is there a way to make them
both "observer" objects so that they can be put in one central list and
updated thru a common interface. (i.e. observer->update( ..))?

Potential solution 1 (multiple inheritence): make a small new observer
class, and two new classes: NewA: derived from OldA, and Observer,..
and NewB: derived from OldB, and Observer. When walking thru a list of
NewA's and NewB's, cast the objects as Observer. (Can this even work?)
and call it's update function.

Potential solution 2 (composition): Make a new abstract composite class
"observer" and derive concrete observers ObserverA, and ObserverB that
contain "instances" of OldA, and OldB respectively. (The problem with
this approach in my case is that OldA is QtWidget and window management
becomes cumbersome.

Potential solution 3: Any other appraoches?

Any comments or rules-of-thumb greatly appreciated.

Thanks,
-Craig

Mar 18 '06 #1
3 1815
"craig" <cr**********@comcast.net> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Given two existing but different classes OldA and OldB (that can not be
made to derive from any new base class); is there a way to make them
both "observer" objects so that they can be put in one central list and
updated thru a common interface. (i.e. observer->update( ..))?

Potential solution 1 (multiple inheritence): make a small new observer
class, and two new classes: NewA: derived from OldA, and Observer,..
and NewB: derived from OldB, and Observer. When walking thru a list of
NewA's and NewB's, cast the objects as Observer. (Can this even work?)
and call it's update function.

Potential solution 2 (composition): Make a new abstract composite class
"observer" and derive concrete observers ObserverA, and ObserverB that
contain "instances" of OldA, and OldB respectively. (The problem with
this approach in my case is that OldA is QtWidget and window management
becomes cumbersome.

Potential solution 3: Any other appraoches?


This is how I do it.
class CGUIElement
{
...

// Observer Model
// OpenObserver listens for when a window Opens
class OpenObserver;
friend class CGUIElement::OpenObserver;

class OpenObserver : public Observer
{
CGUIElement* parent;
public:
OpenObserver(CGUIElement* GUIElement) : parent(GUIElement) {}
void update(Observable* /*Obs*/, Argument* Arg)
{
parent->SetOpen( true );
if ( parent->Type == GUI_List )
parent->ListIndex = dynamic_cast<OpenArgument*>(Arg)->Msg;
}
} OpenObserver;

...
}
Mar 18 '06 #2
In article <11**********************@u72g2000cwu.googlegroups .com>,
"craig" <cr**********@comcast.net> wrote:
Given two existing but different classes OldA and OldB (that can not be
made to derive from any new base class); is there a way to make them
both "observer" objects so that they can be put in one central list and
updated thru a common interface. (i.e. observer->update( ..))?

Potential solution 1 (multiple inheritence): make a small new observer
class, and two new classes: NewA: derived from OldA, and Observer,..
and NewB: derived from OldB, and Observer. When walking thru a list of
NewA's and NewB's, cast the objects as Observer. (Can this even work?)
and call it's update function.
class Observer {
public:
virtual ~Observer() { }
virtual void update() = 0;
};

class NewA : public OldA, public Observer {
public:
void update() {
// call member-functions inherited from OldA
}
};

Potential solution 2 (composition): Make a new abstract composite class
"observer" and derive concrete observers ObserverA, and ObserverB that
contain "instances" of OldA, and OldB respectively. (The problem with
this approach in my case is that OldA is QtWidget and window management
becomes cumbersome.
class NewA : public Observer {
OldA* itsOldA;
public:
NewA( OldA* oldA ): itsOldA( oldA ) { }
~NewA() { delete itsOldA; }
void update() {
// call member-functions of itsOldA
}
};
I would only use the latter if the contained object doesn't need to be
shared. IE, if you need to put a getter in the NewA class to extract
itsOldA for use, then solution 2 is problematic. However, if OldA
already has classes derived from it that also must support the Observer
interface, then you have no choice and must go with solution 2.
Potential solution 3: Any other appraoches?


Is there some ABC that OldA is derived from?

class ParentOfOldA {
public:
virtual void func() = 0;
};

class OldA : public ParentOfOldA {
public:
void func();
};

class NewA : public Observer, public ParentOfOldA {
OldA* itsOldA;
public:
NewA( OldA* oldA ): itsOldA( oldA ) { }
~NewA() { delete itsOldA; }
void update() {
// call member-functions of itsOldA
}
};
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 18 '06 #3
JIm, Thanks for the idea,..This looks interesting, but I don't
completely understand it (most likely due to my lack of C++
sophistication). It looks like you are nesting (embedding) an observer
inside the old object, which can be instanced externally as an
OpenObserver and added to a list of Observers. Then when you want to
call the update function you invoke CGUIElement::OpenObserver->update(
Arg ...) or something similar. If that's the jist of it, I don't quite
undestand what you're doing inside the update definition.
So if you're up to it, any paraphrasing would certainly help!!

Mar 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed...
4
by: Gianni Mariani | last post by:
Two issues here: a) What is the accepted definition of "observer pattern". While I can't point to anything specific, I do remember having issues...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.