Home > Archive > Java Help > October 2005 > doubly linked objects
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
doubly linked objects
|
|
| Andersen 2005-10-20, 7:01 pm |
|
Is there an idiom or pattern for creating two objects (a and b) of two
different classes A and B, such that A contains a reference to b and B
contains a reference to a. Should I pass it through a constructor? In
that case, when creating A, I cannot give the constructor a reference to
b since b has not yet been created (or vice versa if b is created first).
Should I have set methods in both where I can set these variables? Then
I have the problem that one might forget to set it, and the object is in
a bad state.
regards,
Andersen
| |
| Oliver Wong 2005-10-20, 7:01 pm |
|
"Andersen" <andersen_800@hotmail.com> wrote in message
news:4357b934$0$26904$892e7fe2@authen.white.readfreenews.net...
>
> Is there an idiom or pattern for creating two objects (a and b) of two
> different classes A and B, such that A contains a reference to b and B
> contains a reference to a. Should I pass it through a constructor? In that
> case, when creating A, I cannot give the constructor a reference to b
> since b has not yet been created (or vice versa if b is created first).
>
> Should I have set methods in both where I can set these variables? Then I
> have the problem that one might forget to set it, and the object is in a
> bad state.
>
> regards,
> Andersen
Is it possible to have A create B (or vice versa)? e.g.:
<code>
public class A {
private B myB;
public A() {
myB = new B(this);
}
}
public class B {
private A myA;
public B(A a) {
myA = a;
}
}
</code>
You can play around with hiding the constructor of B. Alternatively, you
could hide both constructors and only allow the creation of an A-B pair via
a Factory method.
- Oliver
| |
| Andersen 2005-10-20, 7:01 pm |
| Oliver Wong wrote:
> You can play around with hiding the constructor of B. Alternatively, you
> could hide both constructors and only allow the creation of an A-B pair via
> a Factory method.
I like the last alternative (hide both constructors) as it treats A and
B equally. Problem: if both A and B have private constructors. The
static factory cannot create them either, unless the factory resides in
one of them (uggly). What to do?
| |
| Oliver Wong 2005-10-20, 7:01 pm |
|
"Andersen" <andersen_800@hotmail.com> wrote in message
news:435804CA.70101@hotmail.com...
> Oliver Wong wrote:
>
> I like the last alternative (hide both constructors) as it treats A and B
> equally. Problem: if both A and B have private constructors. The static
> factory cannot create them either, unless the factory resides in one of
> them (uggly). What to do?
Instead of directly hiding the constructors, you can hide the classes
themselves: Make them visible only to other classes in the same package, and
then put them in the same package as the Factory.
You'd probably want to create an interface (or abstract class) for A and
B so that the client code using your factory can do something useful with
the objects they get out of it.
- Oliver
| |
|
| I'm having to use this for my assignment at the moment and I found it easy
to say pass one as a variable and set the other as a method e.g
private B b;
public A(B b)
{
this.b = b;
// rest of constructor
b.addA(this);
}
private A a;
public B()
{
// constructor
}
public void addA(A aInstance)
{
a = aInstance;
}
might work for you might not. you will need to create class B first then A
or swap it round so it makes A first.
JS
"Oliver Wong" <owong@castortech.com> wrote in message
news:TAT5f.28065$yS6.25836@clgrps12...
>
> "Andersen" <andersen_800@hotmail.com> wrote in message
> news:435804CA.70101@hotmail.com...
Alternatively,[color=darkred]
B[color=darkred]
>
> Instead of directly hiding the constructors, you can hide the classes
> themselves: Make them visible only to other classes in the same package,
and
> then put them in the same package as the Factory.
>
> You'd probably want to create an interface (or abstract class) for A
and
> B so that the client code using your factory can do something useful with
> the objects they get out of it.
>
> - Oliver
>
>
|
|
|
|
|