I'm currently working on a Java school project aimed at linking a Java application to a database. We have to use the typical example of a school modelisation, composed of 3 classes: -Teacher -Student -Subject
Each class a it's number of arguments, but the problem that I have is that there is an object of the Subject class in a Teacher (called speciality), and an object of the Teacher class in a Subject(called inCharge), knowing that a Subject is the speciality of is inCharge Teacher.
I have to initialize an ArrayList of Teacher, and I'm trying to do it as follows:
while (rset.next()){
Prof **toInsert** = new Teacher(rset.getInt("NUM_PROF"),
rset.getString("NOM_PROF"),
rset.getString("PRENOM_PROF"),
rset.getString("ADR_PROF"),
rset.getString("CP_PROF"),
rset.getString("VILLE_PROF"),
**new Subject**(rset.getString("CODE"),
rset.getString("LIBELLE"),
rset.getInt("H_COURS-PREV"),
rset.getInt("H_COURS_REA"),
rset.getInt("H_TP_PREV"),
rset.getInt("H_TP_REA"),
rset.getString("DISCIPLINE"),
rset.getInt("COEFF_CC"),
rset.getInt("COEFF_TEST"),
**toInsert**);
ListeProf.add(i, toInsert);
++i;
}
My Teacher class constructor looks like this:
public Teacher(..., Subject speciality){
...
m_speciality = speciality;
}
My Subject class constructor looks like this:
public Subject(..., Teacher inCharge){
...
m_inCharge = inCharge;
}
As you can see, my problem here is that the constructor of the Subject I want to create to be the speciality of the toInsert Teacher needs to use this said Teacher. Is there a way to do both initialization of the objects at the same time, or should I create both objects separately, then bind them to each other using setters?
Sorry for French parts in the code, I tried to translate only the part where help is needed, and thanks in advance for yout answers ^^
Cyclical dependencies between instances should be avoided - not least because you can't actually create the instances, as you have found.
If you really need to have the teacher as a property of the subject, one way you can do it is to have a setter on the Subject
class:
while (rset.next()) {
Subject subject = new Subject(... /* don't specify Teacher as parameter */);
Teacher teacher = new Teacher(..., subject);
subject.setTeacher(teacher);
}
But you should revisit your design to see if you can get rid of them.
The thing which is jumping out here is that you are making the Teacher
a property of the Subject
. This doesn't seem correct to me: for one thing, lots of teachers might teach the same subject. (And each teacher might teach multiple subjects, of course).
The relationship that you're trying to model here looks like:
Teacher
teaches Subject
Subject
is taught by Teacher
I would construct the "is taught by" relationship as something outside the Teacher
and Subject
classes, for example:
// If only one teacher teaches a subject.
Map<Subject, Teacher> isTaughtBy;
So then you do something like:
Map<Subject, Teacher> isTaughtBy;
while (rset.next()) {
Subject subject = new Subject(... /* don't specify Teacher as parameter */);
Teacher teacher = new Teacher(..., subject);
isTaughtBy.put(subject, teacher);
}
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.