Skip to main content
added 128 characters in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

Create one type of object first without adding any references to the other.

Then you create the other type of object, and during its construction you create the bidirectional links between itself and the other object type.

The calling code would look like this:

// create players
Player player1 = new Player("1");
Player player2 = new Player("2");
Player player3 = new Player("3");

// create factions
Faction jpf = new Faction("Judean People's Front");
jpf.addPlayer(player1);
jpf.addPlayer(player3);

Faction pfj = new Faction("People's Front of Judea");
pfj.addPlayer(player1);
pfj.addPlayer(player2);

The implementation of addPlayer would then both add the player to the faction and tell the player that it is now a member of this faction:

public void addPlayer(Player player) {
    this.players.add(player);
    player.addFaction(this);
}

(omitted for clarity: Checking that the player isn't already a member of that faction, and when that's the case, throw an exception)

This example loads the players first and then the factions, but you can of course also do it the other way around. You can also choose to add factions to players instead of adding players to factions. Choose whatever option makes more sense in the context of your game.

Create one type of object first without adding any references to the other.

Then you create the other type of object, and during its construction you create the bidirectional links between itself and the other object type.

The calling code would look like this:

Player player1 = new Player("1");
Player player2 = new Player("2");
Player player3 = new Player("3");

Faction jpf = new Faction("Judean People's Front");
jpf.addPlayer(player1);
jpf.addPlayer(player3);

Faction pfj = new Faction("People's Front of Judea");
pfj.addPlayer(player1);
pfj.addPlayer(player2);

The implementation of addPlayer would then both add the player to the faction and tell the player that it is now a member of this faction:

public void addPlayer(Player player) {
    this.players.add(player);
    player.addFaction(this);
}

Create one type of object first without adding any references to the other.

Then you create the other type of object, and during its construction you create the bidirectional links between itself and the other object type.

The calling code would look like this:

// create players
Player player1 = new Player("1");
Player player2 = new Player("2");
Player player3 = new Player("3");

// create factions
Faction jpf = new Faction("Judean People's Front");
jpf.addPlayer(player1);
jpf.addPlayer(player3);

Faction pfj = new Faction("People's Front of Judea");
pfj.addPlayer(player1);
pfj.addPlayer(player2);

The implementation of addPlayer would then both add the player to the faction and tell the player that it is now a member of this faction:

public void addPlayer(Player player) {
    this.players.add(player);
    player.addFaction(this);
}

(omitted for clarity: Checking that the player isn't already a member of that faction, and when that's the case, throw an exception)

This example loads the players first and then the factions, but you can of course also do it the other way around. You can also choose to add factions to players instead of adding players to factions. Choose whatever option makes more sense in the context of your game.

Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

Create one type of object first without adding any references to the other.

Then you create the other type of object, and during its construction you create the bidirectional links between itself and the other object type.

The calling code would look like this:

Player player1 = new Player("1");
Player player2 = new Player("2");
Player player3 = new Player("3");

Faction jpf = new Faction("Judean People's Front");
jpf.addPlayer(player1);
jpf.addPlayer(player3);

Faction pfj = new Faction("People's Front of Judea");
pfj.addPlayer(player1);
pfj.addPlayer(player2);

The implementation of addPlayer would then both add the player to the faction and tell the player that it is now a member of this faction:

public void addPlayer(Player player) {
    this.players.add(player);
    player.addFaction(this);
}