Re: Associate Objects using domain ID or using object references?
"Veloz" <firstname.lastname@example.org> wrote in message
My question is whether or not you should associated related objects in
your software using a scheme of id's and lookups, or wether objects
should actually hold actual object references to objects they are
For example, lets say you are modelling studen ratings of teachers.
Let's say your applications needs to analyze these ratings for
teachers and is provided a data file. In this data file the teachers
all have unique id's. Each studen'ts ratings of each teacher is listed
under an appropriate section for each student; these ratings carry the
teacher's ID so that you know what the rating is for.
Now, if you were to model this in software, it seems reasonable that
you might end up with a student class, which directly or indirectly
houses the student's ratings for the the teachers. And you would
probably end up with a teacher class and each teacher object would
carry the id assigned to it in the data file.
The question is this: in the class holding the ratings (e.g., the
student class), would you store ratings along with the teacher's ID or
would you actually store references to the proper teacher object?
On one hand, I like the id scheme because it mimics the domain a bit
more, possibly making the app easier to understand and debug (and
possibly to save off and restore state later? I don't know about this
On the other hand, if other objects "point to" feature objects by
holding their ID, you will probably have to do a feature-lookup by ID
to get the actual feature object so that you can interact with it.
I thought this issue was addressed by something called the "IDs versus
Objects" pattern, but I can't seem to turn up anything on that.
Any thouhgts on this topic would be greatly appreciated!!
If the student object contains the teacher ID, then the teacher ID is not
required to even exist if it is not needed. Also, during serialization you
would not have to be concerned with the teacher object still existing at the
time the student object is output. So in the least the student object needs
to contain the teacher ID whether it is used as a key to the teacher object
or not during lookup.
Using maps a lookup is rather quick and simple to do, so it seems that it
wouldn't be too much of a burden to get a refernce to the teacher object as
The main concern I see with storing references to a teacher object is the
fact that the teacher object pointer can not then change. If the teacher
object changes in some manner (it's stored by value in a vector and the
vector is resized, sorted, etc...) the reference would become invalid, as I
understand it. Also, there may be a condition where you don't even have the
teacher object loaded and would only load it from DB as needed. If you are
doing this then you have to ensure that the teacher object that was loaded
remains in memory at the same location until the student object is
I'm not in favor of storing pointers (or references) to instances in my
classes when it is not strictly needed, and when I do I try to limit them to
objects that are created once at the beginning of the program and are
destructed at the end (graphical engine interface, DB connection info,
The problem with not storing a reference, however, now leads us to the
problem of how is the student object going to get access to the teacher
object as needed? But then again, why would the student class ever need
access to the teacher object to begin with? I would not make it the
responsibility of the student class to update the teacher class. I would
lead that responsibilty to either a containing class that kept lists of both
teachers and students, or in mainline.