0

I'm designing a simple game and I'm not sure how to correctly handle the access and relations between classes. E.g:

Class Game - method Start that will initialiaze objects (fields) Player, CPUPlayer, Board.

Player/CPUPlayer contains a method "Place a pawn" but they would need to access the Board object to check for coordinates.

But they do not see the Board object. Does it mean I need to pass the Board object reference (or any other objects) in their constructor?

4

2 回答 2

1

是的,如果它们依赖于该对象,则需要在创建它们时将其注入它们构造函数是正确的地方。稍后,当你掌握了一些东西时,你可能想考虑使用像UnityBoard这样的 DI 容器,但现在,只需在它们的构造函数中接收一个实例。

于 2013-10-30T13:05:53.223 回答
0

在这种特殊情况下,它们似乎确实需要对 Board 对象的引用。如果您想对其进行过度设计,您可以创建一个专用接口 ICoordinateCheckable,在 Board 中实现它,并让 Player 在构造函数中接受此接口的实例(而不是 Board)。

于 2013-10-30T13:51:14.653 回答