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 aBoard
containsPiece
sPiece
->Board
because a piece needs to pass in theBoard
to as a param toMoveBehavior
which needs theBoard
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