Create options within a Java class

Asked

Viewed 33 times

-1

In a game where I have to create a teacher class, and in the creation of each teacher with the specification of the subject, because each one will have a different action.

Example: Math teacher (fold the dots). Portuguese teacher (increases strength). Geography teacher (Zera the points).

How should I create each type of teacher within the class? I don’t need the code, just an idea of how to differentiate the types of each teacher. Thank you

  • 1

    "I don’t need the code, just an idea of how to differentiate the types of each teacher" I understand you’re a model, but isn’t there anything you’ve done to put the question? could create a base class "Teacher", with association to for "Materia" and make the other specific classes inherit from Teacher, as "Professormatematica"

1 answer

0


You could create a "type" in the teacher class where it would be an Enum and there put a logic where it would receive a characteristic interface with its specialties. I’ll post an example of code that’s easier to understand.

public class Professor {

TipoProfessor tipo;


    public TipoProfessor getTipo() {
       return tipo;
    }

}

Type

public enum TipoProfessor {

MATEMATICA(new DobrarPontos()), HISTORIA(new ZerarPontos()), GEOGRAFIA(new AumentarPoder());

private Caracteristica caracteristica;

private TipoProfessor(Caracteristica caracteristica) {
    this.caracteristica = caracteristica;
}

public Caracteristica getCaracteristica() {
    return caracteristica;
}

}

Characteristic interface

public interface Caracteristica {

void getCaracteristica();

}

Specialties

public class AumentarPoder implements Caracteristica {

@Override
public void getCaracteristica() {
    System.out.println("Aumentar força");
}

}

public class DobrarPontos implements Caracteristica {

@Override
public void getCaracteristica() {
    System.out.println("Dobrar pontos");
    
}

}

public class ZerarPontos implements Caracteristica {

@Override
public void getCaracteristica() {
    System.out.println("Zerar pontos");
}

}

Basically the idea is this, you just have to think of a domain name that makes more sense to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.