mercredi 27 juillet 2016

How to remove circular dependency in strategy pattern

I'm trying to grok the strategy pattern and came up with following example

  • Would like to create new games based on chess except pieces moving differently.
  • Also want to use the strategy pattern to inject behaviors (how they can move) into the pieces.

But if we have Board, Piece, and MoveBehavior objects

  • Board -> Piece because a Board contains Pieces
  • Piece -> Board because a piece needs to pass in the Board to as a param to
    • MoveBehavior which needs the Board to decide what moves are acceptable.

How can we get rid of this circular dependency? Also isn't this a common problem in more realistic examples because behaviors need large enough state to make decisions? I think if behavior is super simple I assume it's not that big of a deal to not use strategy pattern.

Aucun commentaire:

Enregistrer un commentaire