how to avoid circular dependencies here

后端 未结 4 1047
悲哀的现实
悲哀的现实 2020-12-14 09:07

Is there a way to avoid circular dependencies, other than mixing modules, in a arrangement like this(it is a chess application)

Long description:

4条回答
  •  遥遥无期
    2020-12-14 09:32

    Consider what each object really needs, and not what it just happens to need at the moment.

    A Piece probably doesn't need to know about a Player - what it needs to know about is something that it can send updates to.

    So, for that example, create an interface representing a "PieceMessageListener" or some such, and have the Player implement that. Now, both concretions are depending upon an abstraction (going to the rule of "concretions should depend upon abstractions, abstractions should not depend upon concretions").

提交回复
热议问题