tags:

views:

62

answers:

5

I'm learning OOP in Java, but I'm not sure how to implement this reference or whatever you want to call it.

There are two relevant classes: Manager and Team

Team contains a field called manager, which is an instance of Manager. This links the team to a manager. It also contains a String field called name, which is the teams name.

Manager contains a String field called name, which is the managers name. It also contains a method called printDetails() which is supposed to print the managers name and team name. The bit I'm stuck with is finding the Team instance for this Manager so I can get her teams name.

I haven't posted any code because I think this is a design feature, and there isn't some magical code to do it for me. (unless you can iterarte through all the Team instances to find the manager)

+3  A: 

Two options:

  • Give the Manager a reference to the Team too
  • Keep a list of all the teams, so you can look through to find which one has the given manager
Jon Skeet
The first sounds easy enough, but is that good practice? It's part of a project for an assignment, so I don't want to hand in bad code.
Matt
@Matt - You may want to follow infinity and just give `Manager` the team name rather than the team. That reduces the coupling. But otherwise, the Manager needs to know about its Team somehow, so this is perfectly valid.
Skilldrick
this can lead to cyclic references. team has a reference to manager and manager has a reference to team. not a recommended design option.
deepsat
@Skilldrick I'll be adding a printDetails() method to Team which will print the managers details, so I'd still have the same problem. I think I'll just give Manager a reference too if you're sure it's the best way. @deepsat Yes, that's what I was thinking but I'm a noob so I don't trust myself..
Matt
@deepsat:why not?
in terms of design if you have two objects having references to each other, it will be hard to remove the coupling. such objects will not be removed by garbage collector and hence lead to memory leaks.
deepsat
@deepsat: You're incorrect. The Java garbage collector can handle this fine. Cyclic references are not a problem for the JVM. They *can* indicate poor design - but certainly not always. In this case it seems pretty reasonable.
Jon Skeet
@Jon - Thanks! My bad. New learning for me!
deepsat
A: 

If the Manager needs to know about the Team, then Team can pass this to Manager in its constructor.

Also, you need to think about who owns who. Does the manager own a team, or does the team own a manager? Maybe in this example, it would be better if Manager had a Team, so it could easily get the details of its team.

Skilldrick
It essentially doesn't make any difference, because I'll be adding a printDetails() method to Team which will print the managers details.
Matt
A: 

If a Manager manages a single team, you can define a field member in Manager class called teamName and set it to the team's name

A: 
class Manager() {
  private Team team;
  private string name;
  private int age;
  public function void Manager(Team t) {
    this.team = t;
  }
}

class Team() {
  private string teamName;
  ...
}
infinity
A: 

Since "Team" contains a reference to "Manager", it can call a method on "Manager" to set the name of the team.

deepsat
That's no good if you only have a reference to the Manager though.
Jon Skeet