jeudi 6 juin 2019

What pattern do I use for interdependent class

I have read from other sources that it isn't a good idea for objects to know about each other especially the ones on same level. It should be more like hierarchy.

My problem is quite unique as I haven't figured out a way around it. Also I have been unlucky to come across any topic that addresses my issue specifically.

The problem I am building a chess app and I am constructing the model of the app. Right now I have an abstract objects like for example Piece which other pieces like Queen, Knight and the rest would inherit from. I also have a Board class which handles all board model and state of a game. Now each of my piece has a generateMove() method to calculate possible moves from their position and to do this they need to know the state of the board. Also the Pieces are been instantiated by Board at startup.

Question Do I go ahead and instantiate Pieces by e.g

public class ChessBoard{
   Boardbit = 64bit
   Knight = new Knight(start_position, this) 

  //calculate

}

and then in Knight class method

public long calculateMove(start_position, ChessBoard);

If no, what other ways can I go about it?

Aucun commentaire:

Enregistrer un commentaire