By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Strong coupling: Object Dependencies

P: 2
/*
Hello everyone,

I am trying to impose some strong coupling between different
objects. This, to a large extent, directly challenge the principle of
encapsulation in C++.

Note that the form of dependencies that I am trying to devise is
between instances of classes, not between classes themselves. Hence, I
have already looked into issues of multiple inheritance, friends and
aggregation, which is not what I want.

Ideally, the member class data within one object could be linked with
the member class data within another object, so that any modification
of the former will result in an updating of the latter.

My application is a statistical software. Below is an example of my
problem with two different classes. A crucial point is that such
strong coupling must be imposable by the user in the main program
only, without modifying or creating a new class for that purpose.

Code Example:
*/

// Classes of random variables:
class Norm
{
public:
int Variate;
int Mu;
int Sigma;

void getMu(){return Mu;}
double getVariates(){
Variate = function(Mu,Sig);
return Variate; }
}

class Gam
{
public:
int Variate;
int Mean;
int Sd;

void getMean(){return Mean;}
double getVariates(){
Variate = function(Mean,Sd);
return Variate; }
}


int main()
{
Norm RanVar_A;
Gam RanVar_B;
double X, Y;

// This is where I need some help to devise an *operator*,
// which would induce some dependencies between two member class
// data, so that updating one, would also update the other.
RanVar_A.Mu *operator* RanVar_B.Variate;

// Ideally, I would then like to have a form of dependence, which
// makes that everytime I call:
X = RanVar_B.getVariates();

// the corresponding Mu in RanVar_A is also updated. So calling,
Y = RanVar_A.getVariates();

// returns a value based computed from the new Mu given by RanVar_B.
return 0;
}

/*
The problem is that the two data that I am trying to associate
are out of scope of each other. I have considered an 'observer design
pattern' approach. However, the instantiation of a 'subject' class,
which, in my specific problem could be called a 'statistical
model' class, does not exactly suit my needs. I don't really want to
update all my objects at once, but simply one relationship at a time.

It seems to me that the problem could easily be resolved by ensuring
that all data in each object are fully public and accessible from
anywhere else, but I don't know how to create such an environment, and
it wouldn't be a viable solution.It seems to me that the problem could easily be resolved by ensuring
that all data in each object are fully public and accessible from
anywhere else, but I don't know how to create such an environment, and
it wouldn't be a viable solution.

Finally, I have also tried an approach where I am passing a pointer to
a data in the 'main' environment to the constructor of each Norm
class, but this has again failed to achieve what I am trying to do.

Any help, or advice is very much welcomed,


Cedric
*/
Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Ideally, the member class data within one object could be linked with
the member class data within another object, so that any modification
of the former will result in an updating of the latter.
You are attempting to re-create the Observer design pattern using another pattern called Mediator.

Check the book Design Patterns by Erich Fromm, et al. Addison-Wesley 1994.

Your answers are on page 293.
Aug 8 '07 #2

P: 2
You are attempting to re-create the Observer design pattern using another pattern called Mediator.

Check the book Design Patterns by Erich Fromm, et al. Addison-Wesley 1994.

Your answers are on page 293.
Thanks for your reply,

So, do you recommend that I try to implement a mediator pattern design, then, rather than an observer pattern one?

Are you sure that this book is by Erich Fromm?
I can't really find anything corresponding to that description.
Thanks,

C
Aug 8 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Check the book Design Patterns by Erich Fromm, et al. Addison-Wesley 1994.
Oops. I meant Erich Gamma. It's early here.

ISBN 0-201-63361-2.

The Observer implements the Notify() so you can let observers know when the data has changed. When there are many Observers, then you may want to use a Mediator to notify the correct observers.

A typical use is where a Person is a data member of a Vehicle as the owner and the Vehicle needs to notify the Registration application if the address of the Person was changed in order to mail notices to the owner at the new address.
Aug 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.