Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get the parent (or container?) of an object?

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, first off, I'm probably using wildly inaccurate terminology in the subject line.

Let's say I have a class called Office, and classes that work with it called Chair, Desk, and Table, such that, in bad pseudocode:



Now, in the program there may be several instances of Office.

Let's say I've got some code in my Desk class that needs to find out something about the Chair that is in the same Office instance. How do I get a reference to the particular instance of Office that the Desk is in?

I have a feeling this is something fairly trivial, but I can't recall how to do this, if it can be done.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a member field that is a reference to its parent, and set it in the constructor:
Of course, you could perhaps use setOffice(Office o) or moveTo(Office o) methods to change the office member.

Of course, it would be best to have some interface or abstract base class for Office, let's say Space, so you can move chairs etc to different kinds of spaces (office, bedroom, etc) without much code change. And you can do the same for Chair, Table, etc. If you make that an abstract class (called Furniture) you can put the moving code into that one abstract base class.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that's the rub.... it's existing code, and I'm not allowed to do that.

So there's no real way given the Desk and absolutely no other information, to get the Office instance that it's inside of?

Sorry, forgot to put that restriction in my original question.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got a collection of all the Office objects? You could look at each one and check its Desk(s).
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. An Office object just contains a reference to a Chair object, and there's nothing in Java that lets you find out what other objects contain references to a particular object.

So, if this is now a requirement (clearly it wasn't before) then the data model will have to change to accomodate it. As per Rob's suggestion perhaps.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really cannot change the current code you can create auxiliary classes that store the relations. Although it's bad practice, it might be the only way.

These classes then keep your connections between spaces and furniture.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the suggestions. So, essentially if Desk x is a Desk that is in Office y, there's no way, given x alone, to find y.

On the other hand, with the problem that I was working on, there did turn out to be a reference to the Office in the first place somewhere.

Buried, of course! Ah, the joys of working on pre-existing code!

Anyway, thanks for the advice and information.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Spoor:
If you really cannot change the current code you can create auxiliary classes that store the relations. Although it's bad practice, it might be the only way.

These classes then keep your connections between spaces and furniture.


Yes, let's say a class called Furniture which has references to all the furniture items.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic