jeudi 27 février 2020

How to manage cross referencing in Object Oriented design ? (chess example)

I am working on a Java project and I would like to a really clean "Object Oriented" design (the best I can...).

But I am struggling to understand how each object should reference the others.

Let's take the simple standard chess game example :

  • Each player has a given number of chess pieces.
  • Each piece belongs to 1 player

In my application, I will probably need to be able to retrieve the information in both way :

  • Access the owner directly from the Piece
  • Access all the owned pieces directly from a player

Here are my questions :

  1. What would be the best design ? Each entity should reference the other one(s) ?
  2. Or X-referencing is just always bad ? Should I always access the objects in "single way" ?
  3. Maybe should I make another class that manage the mapping between the Piece and the Players ?

I do not like the X-ref solution, because it basically duplicate the information and it can potentially create inconsistency and a lot of work overload. But can it be avoided ?

I know those questions are a bit fuzzy but I do not think I can figure out alone ! Please help !

Thanks anyone.

Aucun commentaire:

Enregistrer un commentaire