class MySchool {
ArrayList<MyTeacher> teacher;
...
}
class MyTeacher {
...
}
When I am doing some processing on an instance of MyTeacher, how can I find the instance of MySchool that they belong to? MyTeacher's do not exist independently, they are created by MySchool, however I'd rather not make MyTeacher a subclass of MySchool as this would confuse other parts of the program.
I could save a reference to MySchool in each instance of MyTeacher, but it seems a bit inelegant, I was wondering if there is a simple function that can tell me.
Thanks for the help.
Actually, a reference is the normal way to do it. Every teacher can have a school which is not unique between all teachers, so to know that school, a reference should be added. I don't see why this is not elegant.
Also, it might be possible that a teacher does not have a school, so make the reference null, or in case a teach can have multiple school, you need a list of schools as reference.
If you really do not want this reference, another option is to go through all schools, and search for the teacher you are interested in. If a teacher can teach at one school only, you can stop searching when you found the teacher, otherwise you have to build the list while iterating over all schools and teachers. This is a quite inelegant solution, and should only be used if want to minimize memory. The processing time can increase greatly.