• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

about object reference ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
emily li
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's impossible to answer that question out of context. But there are lots of reasons why that might be a reasonable design decision:
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
emily li
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
... ...

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic