• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Parent child object problem

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code base which I inherited in my current job. fun, Fun , Fun

Persistent, Zoo, Animal are objects

Hospital is a helper class

If you look at Animal you see a instance variable called whichZoo and is of type Zoo

If you look at Class Zoo you see a method call addAnimal(Animal) this sets whichZoo in animal to this and adds to the Zoo
treemap.

Here is the issue look at the Hospital.isSick(animal) method, the first thing it does is create a
Zoo object which loads a all zoos ( yes I mean all) which in turns loads all animals in zoos

this was a crazy loop that was solved via a Utility.loadZoo(int zooId) method that tested the
zooId against the database zooId.

if (db.getZooId()== zooId) { return Zoo }

this was in a while loop going through each zoo in the database.

The whole point was to get the reference to the Zoo from the Animal in a parent child like event.

This allowed all Utility methods to do a Zoo zoo = animal.getwhichZoo();
and be able to use all methods in the Zoo object, only by the if statement does it happen to work but objects in memory
are over burdened and some third party reporting tools loops through object lists which eventually
blows the heap.

Is there a way to get e reference to the parent object and cast it, refactoring is not an option because everything reference Zoo zoo
even when it is only using a Zoo.[method] call but there are cases where it is valid.
There is 100's of these in the programs.

Anyone?



 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example references the San Diego Zoo, one of my favorite places, so I'd love to help. I'm having trouble parsing your question though. In particular:

"only by the if statement does it happen to work but objects in memory are over burdened"

I don't get that at all. Then you ask if it's possible to get a reference to the parent object. If you're asking if it's possible to navigate a one-way relationship in reverse, then pretty much no, you can't do that. You could refactor to make it a two-way relationship. You say refactoring is impossible, but, again, I can't really understand your reasoning.
 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have it conceptually my issue. The Zoo has a list of animals, each animal has a class variable which is of type Zoo. When the animal if added to the Zoo the whichZoo(this) is set prior to the animal.put. The result is every animal in the current Zoo is loaded into the animal.whichzoo which has all Zoo and then has a list of all animals which has a whichzoo (Zoo object) ad nauseum.



The problem is that every method that has a Animal object passed to does the following code. note that line 1 of the method is all over the place in different methods.



The whole idea was to be able to use the animal to get zoo information. animal.zoo.getSomeMethodInZooClass().

Yes a two way object, I believe is what we need. The refactoring I believe would be difficult since the verbiage if Zoo or zoo whther it come from the Zoo Class or the animal.getwhichZoo method which returns a Zoo class.

over 200 methods in 20+ classes either helper class or instance class.

If I was to create a two way object, how do you suggest the linkage between animal and zoo?


 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I was mistaken then. There already is a two-way relationship between Animal and Zoo. Other than a few extra characters, there's no difference between animal.zoo.getSomeMethodInZooClass() that you want to do, and animal.getWhichZoo().getSomeMethodInZooClass(), which you can do. Well, there is an extra method call, but that's not a big deal.

This kind of circular reference really isn't a problem. At least it doesn't suffer from the "ad nauseum" effect as you seem to be defining it.

I do have a couple of suggestions though. A TreeMap is used for sorted collections, and here it's only sorted by the order Animals are added to the Zoo. You're paying a price in performance for no real reason. I'd switch that out for an ArrayList, or possibly a HashMap keyed on the animals description and/or id. Also when you add an animal to a zoo, you should check to see if it's already in a zoo, and either reject the add or remove it from the other zoo's collection. You may want to synchronize the collection or the methods that modify it in order to make the classes thread safe.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic