How to handle a shared object reference?

From: Ken (kk_oop_at_yahoo.com)
Date: 07/31/04


Date: 30 Jul 2004 18:48:43 -0700

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



Relevant Pages

  • Re: How to handle a shared object reference?
    ... > So both hierarchies start with class A. ... > call that class SharedClass. ... where you add a constructor parameter for SharedClass. ... Passing a factory object is a bit cleaner. ...
    (comp.lang.java.programmer)
  • How to handle a shared class?
    ... I have two hierarchies of classes. ... containment. ... call that class SharedClass. ... Could someone recommend an alternative design approach? ...
    (comp.object)
  • Re: How to handle a shared object reference?
    ... Each hierarchy is defined by containment. ... > So both hierarchies start with class A. ... > call that class SharedClass. ... Well your design seems to be that A:SharedInstance is 1:1 ...
    (comp.lang.java.programmer)
  • Re: How to handle a shared class?
    ... > containment. ... > these hierarchies) both use the same instance of another class. ... > call that class SharedClass. ... D and G cannot create their SharedClass objects, ...
    (comp.object)