简体   繁体   中英

interfaces in multiple inheritance in java, modelling issue

I have the following scenario:

在此处输入图片说明

As you can see, I have two classes that are Lecturers and Students. The class Teacher Assistants are a mix between Lectures and Students, that is because they can enrolled into courses, but they can also lecture some basic topics (without being considered Lecturers). I came with the idea to model this situation using Interfaces because I will program in in Java. Is this modelling correct?

在此处输入图片说明

So that the TA class will implement the Interface Teaches, which contains an array of the courses assigned to this student to teach.

But if I model in that way I realize that I am loosing the class Lecturers at all. How I can model this situation of multiple inheritance, but not loosing the class Lecturers? I mean if I program Lecturers as an interface it would not have any methods that I would need further, for example, calculus of its wage benefits and so on. Any recommendation?

You can make Lecturer into an interface, then have both TeachingAssistant and Professor implement it. TeachingAssistant can extend Student , because from a logical point of view, teaching assistants are students.

One option is to have Teacher and Teachable interfaces and then have Lecturer implement Teacher (for want of a word like Teacherable!), Student implement Teachable and TA implement both Teacher and Teachable .

Lecturer , Student and TA could all extend Faculty_Staff .

tbodt's approach , in my opinion, is generally sound. You just have to ask: if some client code needs a Lecturer , would an instance of a TA suffice? If there's an issue with that, you can choose to design it with composition, abstracting the ability to teach into a separate class of its own, something like TeachingJob (I can't think of a better name). This way the only thing that actual Professor s and TeachingAssistant s share is that they have similar teaching jobs; they do not belong to the same "class" (interface actually).

Hybrid approach would be to involve TeachingJob with the Lecturer interface, eg Lecturer would have a assign(TeachingJob assignment) method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM