File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes get the parent (or container?) of an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "get the parent (or container?) of an object?" Watch "get the parent (or container?) of an object?" New topic
Author

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

Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Have you got a collection of all the Office objects? You could look at each one and check its Desk(s).


[Jess in Action][AskingGoodQuestions]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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

Joined: Jan 05, 2005
Posts: 140
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

Joined: Oct 07, 2005
Posts: 637
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.


ASCII silly question, Get a silly ANSI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get the parent (or container?) of an object?