K
Ken
Hi. I'm doing a design using Java that has two hierarchies of
classes. Each hierarchy is defined by containment. So it's something
like:
class A contains an instance of B;
class B contains an instance of C;
class C contains an instance of D;
class A contains an instance of E;
class E contains an instance of F;
class F contains an instance of G;
So both hierarchies start with class A.
Using this example, classes D and G (the classes at the bottom of
these hierarchies) both use the same instance of another class. We'll
call that class SharedClass. D creates, writes to and reads from
SharedClass. G reads from and writes to the same instance of
SharedClass. However, each instance pair of D and G will share a
different instance of SharedClass. In other words, SharedClass is not
a Singleton.
My question is this: How do I make a SharedClass instance available
to both D and G? At first I was going to have A create the
SharedClass instance and have it work its way down to D through the
constructor parameters of B and C, and likewise get it to G via E and
F. However, that would let A, B, C, E and F touch this SharedClass
instance when they don't really need it.
Could someone recommend an alternative approach?
Thanks!
Ken
classes. Each hierarchy is defined by containment. So it's something
like:
class A contains an instance of B;
class B contains an instance of C;
class C contains an instance of D;
class A contains an instance of E;
class E contains an instance of F;
class F contains an instance of G;
So both hierarchies start with class A.
Using this example, classes D and G (the classes at the bottom of
these hierarchies) both use the same instance of another class. We'll
call that class SharedClass. D creates, writes to and reads from
SharedClass. G reads from and writes to the same instance of
SharedClass. However, each instance pair of D and G will share a
different instance of SharedClass. In other words, SharedClass is not
a Singleton.
My question is this: How do I make a SharedClass instance available
to both D and G? At first I was going to have A create the
SharedClass instance and have it work its way down to D through the
constructor parameters of B and C, and likewise get it to G via E and
F. However, that would let A, B, C, E and F touch this SharedClass
instance when they don't really need it.
Could someone recommend an alternative approach?
Thanks!
Ken