Objects referencing each other

  • Thread starter Thread starter Lakenarr
  • Start date Start date
L

Lakenarr

Hi,

I need to have two objects referencing eash other. Something like this:

class Planet
{
private Moon m_Moon;

public Moon Moon
{
get { return m_Moon; }
set { m_Moon = value; }
}
}

class Moon
{
private Planet m_Parent;

public Planet Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}
}

So i can do something like this:

Planet p = new Planet();
Moon m = new Moon();

p.Moon = m;
m.Parent = p;

This looks good for this example but i might need to change these all the
time, think about Husband and Wife classes linking each other with lots of
divorce and marriages.

I could do something like this:

class Planet
{
private Moon m_Moon;

public Moon Moon
{
get { return m_Moon; }
set
{
if (m_Moon == value)
return;

m_Moon.Parent = null;
m_Moon = value;

if (m_Moon != null)
m_Moon.Parent = this;
}
}
}

So this would do the trick:

Planet p = new Planet();
Moon m = new Moon();

p.Moon = m;

But what if someone changes m.Parent, expecting it to assign that, i could
have an "internal" setter on Moon's setter of Parent but it doesn't solve my
problem in the assembly.

Is there any common way to do this? What would you suggest?

Thanks.
 
Lakenarr said:
[...]
But what if someone changes m.Parent, expecting it to assign that, i
could have an "internal" setter on Moon's setter of Parent but it
doesn't solve my problem in the assembly.

Is there any common way to do this? What would you suggest?

Could you be more specific about what problem it is you're trying to solve?

It seems to me that it's perfectly reasonable to just require that code
that changes one property remembers to change the other instance's
property. For example:

Person husband, wife;

husband.Spouse = wife;
wife.Spouse = husband;

Then later when they get divorced:

husband.Spouse = null;
wife.Spouse = null;

What's wrong with that?

There are a variety of other ways to do something like this, and you've
posted one alternative, but since it seems to me that you haven't
clearly stated the goal you want to achieve, it's not possible to say
whether the alternative you've posted is appropriate and/or whether
there's a better way.

Pete
 
Lakenarr:

The problem you are trying to solve comes up quite a bit in OO design. The
common solution is to have another class that captures the
association/relationship.

For example, Husband and Wife are classes, and MartialRelationship is
another class that captures the association. This MaritalRelationship class
can then track interactions between the parties, e.g., it might contain a
collection of events such as Married, Separated, Divorced, etc. This
technique is often used to track changes in state over time as well.

For more information, check the Web for associatioin classes or link
classes.

Hope this helps,
jpuopolo
 

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

Back
Top