Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to handle a shared object reference?

Reply
Thread Tools

How to handle a shared object reference?

 
 
Ken
Guest
Posts: n/a
 
      07-31-2004
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
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      07-31-2004

"Ken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?
>


Well your design seems to be that A:SharedInstance is 1:1

So either do what you are doing or make SharedInstance a multiton, which
tracks its own instances in a Map keyed by instances of 'A'.

So a job for 'A' is to create these instances, then SI can get them.

I prefer your approach though. Seems that 'A' must do the instantiation
whatever.

--
Mike W


 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      07-31-2004
"Ken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


These are not hierarchies as you've described them.
Hierarchy is a term for inheritance. You've indicated
that the classes CONTAIN AN INSTANCE. Therefore, this
is not "IS-A" relationship, but "HAS-A" relationship.

> 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.


Start looking at the bottom (D,G) and see where these
instances are instantiated, then proceed upward. At
the point of divergence into "main stream" code, that's
where you add a constructor parameter for SharedClass.
Most likely, it is the constructor for A.

> However, that would let A, B, C, E and F touch this SharedClass
> instance when they don't really need it.


Instead of passing an instance of SharedClass from A
all the way through (D,G), instead pass an instance
of a SharedClassFactory object.

> Could someone recommend an alternative approach?


Passing a factory object is a bit cleaner. The intermediate
classes can just pass the reference straight through to the
lower levels. At the lowest level, the constructors for (D,G)
can call the factory object to return the SharedClass instance.

The factory object should have an implicit reference to
the "A" instance that started the chain of instantiations,
so that when the factory is called to dispense an instance
of SharedClass, it can use it's internal reference to "A"
to obtain the same instance of SharedClass for both the
"D" and "G" constructors. You may also want to throw an
exception when the factory has already dispensed the same
SharedClass instance twice (once for "D" and again for "G").
Don't bother saving the factory instance. When the "D" and
"G" constructors are finished with it, let the garbage
collector discard it.

2 cents worth. Your mileage may vary.


 
Reply With Quote
 
kk_oop
Guest
Posts: n/a
 
      07-31-2004

Ken wrote:

>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.
>

Based on everyone's input, here's what I'm thinking of doing.

I'm going to have class A implement an interface called
FamilyInstanceId. This will have one method called "getInstanceId"
which will look like this:

abstract public Object getInstanceId( );

Class A will implement it like this:

public Object getInstanceId( );
{
return (this);
}

So this basically returns an instance of the family owner class as an
object. All the classes in the chains below class A will have
FamilyInstanceId as a constructor parameter:

class B
{
public B(FamilyInstanceId anAFamilyInstanceId )
{
this.c = new C(anAFamilyInstanceId);
}
...
}
...
class D
{
public D(FamilyInstanceId anAFamilyInstanceId )
{
this.A_FamilyInstanceId = anAFamilyInstanceId;
}

I could then define SharedClass as what someone in this thread called a
"multiton." The multiton getInstance method would have a parameter of
FamilyInstanceId:

public SharedClass getInstance(FamilyInstanceId anAFamilyInstanceId);

getInstance would return the instance of SharedClass associated with
this particular family instance, e.g., the family under a particular
instance of A.

Doing this will enable any family member under a single instance of A to
access the family's shared instance of SharedClass. The nice thing
about this is that if additional shared classes are needed, they can
follow the same pattern, e.g.:

public SomeOtherSharedClass getInstance(FamilyInstanceId
anAFamilyInstanceId);

This scales nicely since it will avoid having to add additional
constructor parameters for additional shared class factories.

Also, other family's beyond this one can use the same approach, since
the name of the FamilyInstanceId class is generic.

Any thoughts?

Thanks,

Ken








 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package to handle table text render (handle space or tab betweenthe columns) ? =?ISO-8859-1?Q?KLEIN_St=E9phane?= Python 3 10-06-2006 08:46 AM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM
built with --enable-shared but get error: libpython2.3.so.1.0: can't open shared object file Douglass Turner Python 2 09-04-2003 01:56 PM



Advertisments