0

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,

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
LostReality
  • 657
  • 2
  • 8
  • 33
  • Here are a few references concerning [circular dependencies](https://stackoverflow.com/questions/37444940/spot-problems-with-circular-dependency/37445480#37445480). – jaco0646 Nov 15 '19 at 00:34

2 Answers2

3

You're right to feel uncomfortable with this. The first question is do you need references in both directions? And a team has many employees, but an employee can only be on one team. Is this correct, or can the association be many-to-many? As this association is getting quite complicated, I'd be inclined to create a separate class for it, perhaps called TeamMemberhip. This is typical practice in OOP -- when something becomes complicated, try to separate that complexity out into a class on its own. For example, in the GoF Design Patterns book, this is the recommended approach when the Observer-Subject association becomes many-to-many. Hope this helps.

ComDubh
  • 793
  • 4
  • 18
3

Is there any "cleaner way" to achieve that ?

Example 1:

Common practise - Only Team knows which employees it has.

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

public class Employee {
  private String name;
}

Example 2:

Separate Team and Employee completely from each other and create a new class which will take care of their relation(s). In that way, there is only one place of truth and you can allocate an Employee to multiple teams if you want so.

public class Team {
  private String teamName;
}

public class Employee {
  private String name;
}

public class Membership {
  private Team team;
  private List<Employee> employees;
}
Neo
  • 1,869
  • 1
  • 7
  • 20
  • 1
    @LostReality, you have accepted this answer, but it doesn't address your need to "directly get the linked team of a given employee", as you explained in your question. Futhermore, the title you gave to the question seems to ask whether there is any good use case for having cross-referenced classes. This answer doesn't clearly say 'yes' or 'no' to that question. Finally, you asked "What questions should I ask whenever I am facing that kind of problem?", but you didn't get an answer. Are you really satisfied with this answer? – www.admiraalit.nl Nov 14 '19 at 13:40