Alex Rivera | Logout

Is it bad OOP practice to have objects reference each other?

Asked 2010-05-12T17:31:42.807
28

Pardon my noobness. I'm making a game in which several characters have relationships with each other and they need to be able to interact with each other and store some relationship data regarding how they feel about each other.

I have an object for each character. Is it bad for each of those character objects to have an array of all the other character objects in order to perform these interactions? Is there a better way to do this?

EDIT to answer questions: My language is C#.

What I was thinking is that the setup class would make an array of all characters and then pass that array as a parameter to the constructor of each character.

As for the types of relationships, there are five characters, but I would like to make it extendable in case more are added later. The characters have conversations with each other. What they say is affected by their mood and how they feel about the character. Also, what another character says to them affects their mood and how they feel about the character. They can have one-on-one conversations or talk in groups, so character A could say something to character B, C, D, E, and F all at once.

By the way, I'm learning so much for from this thread. Thank you SO much, everyone!

oop
Edit
Report

1 Answer

8

In OO design, a common approach to a problem like this is the Mediator Pattern

A problem with the approach you describe is how will the relationships be updated? For example, if character A is removed from the game, how are other characters updated to remove character A from their relationships?

I would suggest having another Object that manages the relationships. This object can then be responsible for updating relationships. Using the same example, when character A is removed, the relationship manager simple removes all relationships involving that character.

answered 2010-05-12T17:50:07.000

Your Answer