my dog learned polymorphism
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
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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: 129
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: 19232

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
How To Ask Questions How To Answer Questions
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 129
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: 24081
    
  15

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: 16487
    
    2

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: 19232

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: 129
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: get the parent (or container?) of an object?
 
Similar Threads
Why static variables can not be overridden, Any Example
Dare you to...
Looping a switch statement?
Is it possible to tell Hibernate to modify an existing entity instance
Office Dares !!!