Pergunta

So imagine I have a Superclass Staff which has generic variables such as Name, address etc. There are 4 subclasses Doctor, Nurse, Receptionist and OfficeManager each with unique variables and methods. The problem with this set up is, a Doctor can also be an OfficeManager for example, so would need access to the variables and methods in OfficeManager Is there a way an instance of Doctor can have access to methods in OfficeManager? Is having Staff as a superclass and the 4 subclasses an ideal design?

Any other ideas would be appreciated.

ps. I haven't started drawing up the code as I still don't know the design stage, ie Classes.

Foi útil?

Solução 2

If several subclasses need the same functionality, that functionality should be moved either to the Staff class or some level between Staff and e.g. Doctor. It depends on the functionality and intent. The subclasses should not be aware of each other though, that kind of ruins the whole idea.

Outras dicas

If a Doctor can also be a OfficeManager they should definitely be interfaces. That way you can have something like:

public class VeryImportantPerson implements Doctor, OfficeManager {

Otherwise your approach with a Staff interface and 4 other ones extending them sounds just fine.

Use interfaces. Have your interface require that you implement get methods for each one of the properties that you wish to have in common among your classes. You can then implement multiple interface in a concrete or even an abstract class.

If a class can be both a Doctor and an OfficeManager, then they shouldn't be classes. You might make them interfaces.

Alternatively, you might change your class hierarchy to represent responsibilities instead of people. Then you could have a Person class that contains a List of responsibilities.

Create an interface for each subclass , with the functions it can do , that when you create a class of a doctor implements the "Manager" Interface , it will have access to the manager functions if you need something more complex , you might want to reconsider the hierarchy like , for example

*Staff
 -management
    *Manager
    *recieption

  -practioner
    *doctor
    *nurse

You should create an ENUM called ROLE which has enum values like: DOCTOR, NURSE, OFFICE_MANAGER, and an instance of a Staff should hold a set of ROLEs.

This way for example in a method that is found in Staff class you can:

public void fireEveryoneAndSetTheBuildingOnFire(){
    if(!thisPersonHoldsOfficeManagerRole()){
        // Exception here.. Required role not found..
    }

    fireEveryone();
    setTheBuildingOnFire();
}

Maybe think about the jobs themselves as roles that can decorate a staff object.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top