jeudi 14 novembre 2019

Is there good usecases of crossreferenced classes in OOP ? (A class referencing B, B referencing A)

I am doing a personal project in Java and I would like to make a really "clean" code and classes design.

I am asking myself if this design is would be OK in this case :

public class Team {
  private String teamName;
  private List<Employee> team;
}

public class Employee {
  private String name;
  private Team parent;
}

I made this design because I can directly retrieve all employees of a given team through Team object but also directly get the linked team of a given employee. But the disadvantage I find is it can create incoherent references.

Is there any "cleaner way" to achieve that ? What questions should I ask whenever I am facing that kind of problematic ?

I know it's a quite generalist question but I have no methodology concerning entities relationship in OOP.

Thanks for any help,

Aucun commentaire:

Enregistrer un commentaire