thed0ctor
thed0ctor

Reputation: 1396

pattern for building objects that contain other objects which contain their parents

This question is difficult to articulate but here goes:

I have a set of classes with the following relationship :

diagram

The issue comes when I'm trying to access the data. I'm trying to do this as efficiently as possible. The user should be able to look up a course and its sections, a professor and its sections, a section and its students (implying a professor/student relationship), a section and its professor (a section doesn't have more than one professor). Also sections don't care about which courses they belong to but courses do care about their sections.

I should be able to say, for example, professorA.listSections(); or professorA.listSections() etc.

I'm honestly not sure how to go about doing this in an efficient manner (if there is one). Any design patterns or ideas would be greatly appreciated.

Upvotes: 1

Views: 63

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53525

It looks like you already mapped out most of the things, now all you have to do is connect between the entities:

  • Each professor object should have a list of courses that he teaches.
  • Each Student object should have a list of courses that he studies.

But on the Student case - he should have something "extra": since every course could be taught by more than one Professor, the Student needs a way to "know" which professor teaches the course each he takes (another "extra" field).

I'm not sure I understand the sections thing - but it can be done in the same manner.

Upvotes: 1

Related Questions