U
U
Urukhayy2016-11-13 14:21:54
Programming
Urukhayy, 2016-11-13 14:21:54

What is the best way to solve this OOP problem?

There are classes: University and Professor.
University contains many instances of Professor.
Professor must have a getUniversity method that returns University.
How can you solve this problem in OOP? At the same time, Professor should not store a link to a university instance, because this is an aggregation, and a professor can exist in the absence of a university, so a link to a university can always be empty. Moreover, the principle of SOLID (OCP) is violated: if in the future we want to add College and the professor will have to work in two educational institutions at once.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2016-11-13
@GavriKos

Will your professor's GetUniversity method break SOLID and aggregation? In my opinion, here is the usual DI - inject the dependency into the professor, just not through the constructor, but through the field, since the professor can run back and forth from university to college.

A
Artem Spiridonov, 2016-11-13
@customtema

University contains many instances of Professor.
What? A university can spawn many instances of universities, and a professor can spawn professors. And they cannot inherit - the professor has neither the properties nor the methods of the university.
As the colleague correctly suggested above, belonging to the university is a common property of a professor, and it can be many-to-many. Hence, the answer to your question is: a regular getter.

A
asd111, 2016-11-14
@asd111

one . Make a separate array of university - professor dictionaries. When Colleges appear, another college-professor array will be needed. To search, you will need to bypass the entire array. Changing the class of the professor will not be necessary when colleges appear.
2. Keep an array of professors in each university and an array of universities in each professor. When adding colleges, you will need to change the class of the professor.
The first solution is more universal, corresponds to the normal forms of database design and is often used in database design, i.e. when the database appears, and it seems to appear, then it will be easy to transfer everything to the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question