newbie to OOP design question - communications between objects

M

mati-006

Hi
Suppose we have classes A,B and C, and A must call some member f-ns of
B, B calls something from C and C something of A.

There is some object that contains A a; B b; and C c;
Objects a, b and c has pointers (in object "a" I've got a pointer to
"b", and so on), and everything is fine.
Question is - how to initialize such pointers. I can't do it in
constructor (maybe I could if simulatenous initialization of these three
objects would be possible ;-), I could initialize these objects and then
call some get_pointer() functions that will finish the initialization,
but this approach seems "ugly" to me (and could get messy when program
is getting more complicated). Is there a good and proper OOP approach to
this?
 
D

Daniel T.

mati-006 said:
Hi
Suppose we have classes A,B and C, and A must call some member f-ns of
B, B calls something from C and C something of A.

There is some object that contains A a; B b; and C c;
Objects a, b and c has pointers (in object "a" I've got a pointer to
"b", and so on), and everything is fine.
Question is - how to initialize such pointers. I can't do it in
constructor (maybe I could if simulatenous initialization of these three
objects would be possible ;-), I could initialize these objects and then
call some get_pointer() functions that will finish the initialization,
but this approach seems "ugly" to me (and could get messy when program
is getting more complicated). Is there a good and proper OOP approach to
this?

class SomeObject {
A a;
B b;
C c;
public:
SomeObject() {
a.set_b( &b );
b.set_c( &c );
c.set_a( &a );
}
};

Frankly, it's a pretty strange setup, I would look for a different
design.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

mati-006 said:
Hi
Suppose we have classes A,B and C, and A must call some member f-ns of
B, B calls something from C and C something of A.

There is some object that contains A a; B b; and C c;
Objects a, b and c has pointers (in object "a" I've got a pointer to
"b", and so on), and everything is fine.
Question is - how to initialize such pointers. I can't do it in
constructor (maybe I could if simulatenous initialization of these three
objects would be possible ;-), I could initialize these objects and then
call some get_pointer() functions that will finish the initialization,
but this approach seems "ugly" to me (and could get messy when program
is getting more complicated). Is there a good and proper OOP approach to
this?

You don't give a lot of details about what the problem domain actually
is and the proper answers to these sorts of question are domain
specific.

As somebody else pointed out, you may have a bad design, or you may
have mis-analysed the situation.

When you get these circular dependancies normally there are two
approaches:

1. There is a temporal element and the context should naturally be
passed along with the messages. For example if A uses B first then A
tells B which instance is invoking the method. B then passes this on to
C which uses it when it invokes A's method - a bit like a callback
mechanism.

2. You use the common container as a mediator. You can think of it like
a switch. Each of the sub-objects route their requests through the
common superstructure which then passes the message on. So if Z is the
class that contains the instances of A, B & C then A calls a method not
on an instance of B directly, but on the instance of Z which then knows
where to pass it on to.

You also don't have to use initialiser lists. Under some circumstances
a two stage construction is perfectly acceptable - especially where the
coupling between the classes is naturally high (which it sounds like
you may have). Either of the approaches above will help to reduce this
coupling though.

Fundementally what you are fighting is a limitation in the language
design. There are good reasons for the limitation, but it means that
your real issue isn't a general OOP one, but a syntactic issue in C++
(which I'm sure you've already realised).

Normally though this points to a weakness in the design and I expect
you'll get better results if you consider one of the two approaches
above, although this does depend critically on exactly what the context
of the problem is.


K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top