Home > OS >  Object Oriented Model
Object Oriented Model

Time:11-20

I have a question and answer related to the Object oriented concept and I need help to understand the answer. Below is an example where I have 2 classes 'Student' and 'Course'. I dont need code just the concept of how object model works.

classes relation diagram

Question: How would I get all the titles of the courses a student is taking?

Answer: From Student, I would traverse the relationship to Course and when I get to the Course I return the title of the Courses.

What does it mean to traverse the relationship? Doesn it mean to create List of objects of Student class inside Course class?

CodePudding user response:

The relationship between Student and Course has to be modeled somehow. One possibility is to give each Student a field Collection<Course> courses (and expose it through a getter). Traversing this relationship means to get this field, i.e. to call the getter.

CodePudding user response:

The association between Student and Course means that for each instance (object) of Student there may or not be one or more links to instances of Course. Formally, a link is defined in UML to be a tuple that identifies the objects at each end.

"Traversing" the association means to find for a Student s, all the Course c, with which s is linked. How it is done is not specified, but there are two popular ways to implement this:

  • Each Student object keep a collection of courses linked with them. In this case, traversing the association would consist of iterating through the collection.
  • Separate objects keep track of the links that may exist between a Student and a Course. Traversing the association would then consist of finding the subset of links with a given Student at one end, and iterating through this subset to find the linked Course.

Note: the notion of traversing an association is related to the UML concept of navigability. A navigable association from Etude to Course, means that it's easy to traverse the association in this direction. Navigability can be unidirectional (e.g. if there is no efficient way for a course to find the linked students).

  • Related