How to initialize to "intertwined" objects in Java at the same time? -


i'm working on java school project aimed @ linking java application database. have use typical example of school modelisation, composed of 3 classes: -teacher -student -subject

each class it's number of arguments, problem have there object of subject class in teacher (called speciality), , object of teacher class in subject(called incharge), knowing subject speciality of incharge teacher.

i have initialize arraylist of teacher, , i'm trying 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 this:

public teacher(..., subject speciality){     ...     m_speciality = speciality; } 

my subject class constructor looks this:

public subject(..., teacher incharge){     ...     m_incharge = incharge; } 

as can see, problem here constructor of subject want create speciality of toinsert teacher needs use said teacher. there way both initialization of objects @ same time, or should create both objects separately, bind them each other using setters?

sorry french parts in code, tried translate part needed, , in advance yout answers ^^

cyclical dependencies between instances should avoided - not least because can't create instances, have found.

if really need have teacher property of subject, 1 way can have setter on subject class:

  while (rset.next()) {     subject subject = new subject(... /* don't specify teacher parameter */);      teacher teacher = new teacher(..., subject);      subject.setteacher(teacher);   } 

but should revisit design see if can rid of them.

the thing jumping out here making teacher property of subject. doesn't seem correct me: 1 thing, lots of teachers might teach same subject. (and each teacher might teach multiple subjects, of course).

the relationship you're trying model here looks like:

  • teacher teaches subject
  • subject taught teacher

i construct "is taught by" relationship outside teacher , subject classes, example:

// if 1 teacher teaches subject. map<subject, teacher> istaughtby; 

so like:

  map<subject, teacher> istaughtby;   while (rset.next()) {     subject subject = new subject(... /* don't specify teacher parameter */);      teacher teacher = new teacher(..., subject);      istaughtby.put(subject, teacher);   } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -