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

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!


It's not bad practice for objects to retain references to each other - it's not uncommon for nodes in a tree graph to keep a reference to their child nodes, and for the child nodes to keep a reference to their parent node.

One thing that might become an issue is what you describe as each character keeping references to all other characters. That's an NxN matrix, and will explode your memory use as the number of characters increases.

An alternative to storing a list of all character objects in each character object might be to set up an associative list or dictionary where the key is (character 1, character 2) and the content is the relationship. This list could be global and store all relationships between all characters. The advantage here is that it grows linearly with the number of actual relationships, not exponentially with the number of characters in the system.


It's generally good to avoid unnecessary references between objects, but the determination of what is unnecessary is very subjective and depends on the problem.

If every character has a unique identifier (eg, a unique name), you could merely store, for each character, a collection of character identifiers and the "feeling" about that character. Using some other CharacterManager type, you could lookup a character based on the identifier.

Another option (may be preferable if opinions are just one minor facet of the game) is to have a separate "RelationshipManager" class, that can answer queries such as "How does chararacter X feel about character Y"? This might be a better design since that responsibility is managed separately from your core character. This manager could directly reference characters, or it could use the identifiers.


No, this is not a bad practice at all. Obviously you could do it in such a way as to make it difficult to maintain or understand, but there's nothing inherently wrong with references between objects.

Though I would recommend whatever collection type your language provides rather than an array, there's nothing wrong with what you propose.

链接地址: http://www.djcxy.com/p/51036.html

上一篇: 面向对象的国际象棋游戏设计

下一篇: 让对象互相引用是不好的OOP练习?