aspose file tools*
The moose likes Java in General and the fly likes about object reference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "about object reference ?" Watch "about object reference ?" New topic
Author

about object reference ?

emily li
Greenhorn

Joined: Dec 18, 2002
Posts: 6
i saw a class with the following structure:

in the real application, they are more complicated. but they do have the structure like above: Line has Point as member, and Point has Line as member as well to make some operation easier.

my question is: is this kind of structure good? or proper? any performance, or potential problem there?
it really confuses me... as it looks like a kind of infinite reference: Line -> Point -> Line -> Point -> ...
thanks,
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

It all depends on the relationship between your classes. Two classes having a one-to-one relationship may refer to each other. Imagine that a person can only have one dog, and a dog can only have one loving master.

A Person called "emily" may have a Dog called "wiz". "emily" knows about "wiz" and "wiz" knows about "emily".

(I'm not discussing whether or not the design of your Point and Line classes is good or not)


[My Blog]
All roads lead to JavaRanch
emily li
Greenhorn

Joined: Dec 18, 2002
Posts: 6
thanks for your reply. but i understand "it depends on the relationship between them". maybe my sample is bad.

what i want to know is: is this design correct, or good?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's impossible to answer that question out of context. But there are lots of reasons why that might be a reasonable design decision:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

We don't know what these Points and Lines are for in your application, nor do we know how they should be related.

Your design makes it so that a Point can only be in one Line. There are lots of situations where that would be wrong -- for example if a Point could be at the intersection of two Lines. But maybe that doesn't apply in your application. We can't tell.
emily li
Greenhorn

Joined: Dec 18, 2002
Posts: 6
sorry, my bad example.

just forget the line & point. let me say just 2 objects.

A & B have some relationship. i know this design make the usage later much much easier. i can just use B.aMemberOfB (while inside B) to access the member/method of class A. but is that a good design?
does aMemberOfB keep the reference of A or a whole copy? will this design cause any performance problem?
i worked on C for a while. i know this kind of structure is not allowed in C (or maybe just hard to implement in C). that's why i comes up this question.

sorry, i didn't make myself clear. hope this help to clarify my question/concern.

thanks,
Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73
emily li wrote:...
... but is that a good design?
does aMemberOfB keep the reference of A or a whole copy? will this design cause any performance problem?
i worked on C for a while. i know this kind of structure is not allowed in C (or maybe just hard to implement in C). that's why i comes up this question.

sorry, i didn't make myself clear. hope this help to clarify my question/concern.

thanks,

This post was more clear, i think. it just keeps a reference to the object instantiated from class A ( or made of A ! ) and not only cause any problem, but makes sense to interaction between objects: that's the way they can call each other!
You can just figure it out by answering the question:

how to call a method of class A from within class B ?
of course you need to have a reference to A to point to it from B (or anywhere outside A)

Look at another example i think will make more sense even:


I'm really tired of being engaged with stuff other than Java and programming

Wish to get back soon to my love...
emily li
Greenhorn

Joined: Dec 18, 2002
Posts: 6
I see. thank you

Esmaeil Ashrafi wrote:
... ...
This post was more clear, i think. it just keeps a reference to the object instantiated from class A ( or made of A ! ) and not only cause any problem, but makes sense to interaction between objects: that's the way they can call each other!
You can just figure it out by answering the question:

how to call a method of class A from within class B ?
of course you need to have a reference to A to point to it from B (or anywhere outside A)
... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about object reference ?