Let's say I'm implementing my own version of Scrabble.
I currently have a Board
class that contains lots of Squares
. A Square
in turn is composed of a IBonus
and a Piece
. The bonus implementations are actually the usual bonus for Scrabble, but it is possible that I might try to add some new and twisted bonus to spice the game -- flexibility here is paramount!
After thinking for a while I came to the conclusion that for IBonus
implementations to work, they'll need to know the whole Board
and also its current position(on the Board
, so it knows where it is and it can check for the piece that's in the same square as the bonus is). This strikes me as bad as basically it needs to know a whole lot of information.
So, my naive implementation would be to pass the Board
as argument to IBonus.calculate()
method, IBonus.calculate(Board board, Point position)
, that is.
Also, it seems to create a circular reference. Or am I wrong?
I don't particulary like this approach, so I am looking for other possible approaches. I know I can make calculate
accept an interface instead of a concrete class, i.e., calculate(IBoard board)
but I IMO that isn't all that better than the first case.
I fear being too focused on my current implementation to be able to think of whole different designs that could fit at least as well as solutions to this problem.
Maybe I could re-architect the whole game and have the bonuses in other place, so it would facilitate this calculation? Maybe I am too focused on having them on the Board
? I certainly hope there are other approaches to this problem out there!
Thanks