【发布时间】:2011-02-22 02:39:16
【问题描述】:
我定义了如下接口:
public interface IStateSpace<State, Action>
where State : IState
where Action : IAction<State, Action> // <-- this is the line that bothers me
{
void SetValueAt(State state, Action action);
Action GetValueAt(State state);
}
基本上,IStateSpace 界面应该类似于棋盘,并且在棋盘的每个位置上,您都有一组可能的动作要做。这里的这些动作称为IActions。我以这种方式定义了这个接口,因此我可以适应不同的实现:然后我可以定义实现 2D 矩阵、3D 矩阵、图形等的具体类。
public interface IAction<State, Action> {
IStateSpace<State, Action> StateSpace { get; }
}
IAction,将向上移动(如果在(2, 2) 中移动到(2, 1)),向下移动,等等。
现在,我希望每个操作都可以访问 StateSpace,以便它可以执行一些检查逻辑。这个实现正确吗?或者这是循环依赖的坏情况?如果是,如何以不同的方式完成“相同”?
谢谢
【问题讨论】:
-
你刚刚让我的大脑爆炸了一点。
-
可以添加IAction
的定义吗? -
嗯,它就在那里,第二个代码块。
标签: c# generics oop circular-dependency