473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1880
"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 by multiple threads without using the lock...
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 with inconsistency in the definition. b)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.