对象在一个列表中使用不同的继承接口
目标
我想让许多演员使用可以在运行时交换的各种选择器在3D场景上行动。目前,有两种选择器,即在位置上行动的选择器,以及对象对物体作用的选择。例如,删除器将使用各种对象选择器(只有蓝色对象,只有圆形对象,所有连接的对象等)对对象作用。
接口:
public interface IPosSelector {
HashSet<GridPos> SelectedPositions { get; }
}
// this is really just a slight addition to IPosSelector,
// much of the functionality is the same, hence the inheritance
public interface IObjectSelector : IPosSelector {
HashSet<GridObject> SelectedObjects { get; }
}
public interface IActor {
IPosSelector Selector { get; }
}
使用这些接口的类:
// Actors use the selectors
public class ObjectActor : IActor {
IObjectSelector Selector { ... } // error because of wrong return type (wants IPosSelector)
}
public class PosActor : IActor {
IPosSelector Selector { ... }
}
// keeps track of them and switches the Selectors on them at runtime
public class Actors {
List<IActor> actors;
actors[0].Selector = ....
}
问题
我如何整理这些组件之间的关系?我在这里的设计有点困扰,我觉得这应该更简单。如书面,代码不起作用,因为每个iActor
都需要实现iposselector选择器
,而不是iobjectSelector
的继承接口之一。因此,想到了一些想法,但是每个想法都有一些问题:
- make
iActor
generic ... egiactor&lt; t&gt;其中t:iPosselector
- 但是,我该如何将它们放在列表中?我需要一个常见的接口,需要在那里有Iselector,因此原始问题仍然存在。
- 使所有
iActor
实现使用基本iposselector
,然后在适当的情况下内部施放到iobjectSselector
。- 但是结构应强制执行哪种演员使用哪种选择器。虽然我可以在属性设置器中进行键入检查...
The Goal
I want to have a number of Actors that act on a 3D scene using a variety of selectors that can be exchanged at runtime. Right now there are two types of Selectors, those that act on Positions and those that act on Objects. E.g. a DeleteActor will act on objects using a variety of Object selectors (only blue objects, only round ones, all connected objects etc).
The interfaces:
public interface IPosSelector {
HashSet<GridPos> SelectedPositions { get; }
}
// this is really just a slight addition to IPosSelector,
// much of the functionality is the same, hence the inheritance
public interface IObjectSelector : IPosSelector {
HashSet<GridObject> SelectedObjects { get; }
}
public interface IActor {
IPosSelector Selector { get; }
}
The classes using those interfaces:
// Actors use the selectors
public class ObjectActor : IActor {
IObjectSelector Selector { ... } // error because of wrong return type (wants IPosSelector)
}
public class PosActor : IActor {
IPosSelector Selector { ... }
}
// keeps track of them and switches the Selectors on them at runtime
public class Actors {
List<IActor> actors;
actors[0].Selector = ....
}
The problem
How can I organize the relationship between these components cleanly? I am getting a bit stuck with the design here and I feel like it should be simpler. As written, the code doesn't work because every IActor
needs to implement IPosSelector Selector
, not one of the inherited interfaces like IObjectSelector
. So a few ideas came to mind but there are some issues with each:
- Make
IActor
generic... e.g.IActor<T> where T : IPosSelector
- But how do I do put them in a list then? I would need a common interface which would need to have ISelector in there so the original issue remains.
- Make all
IActor
implementations use the baseIPosSelector
, and then cast internally toIObjectSelector
where appropriate.- But the structure should enforce which Actor works with which kind of Selector. Though I could do type checking in the property setter...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论