对象在一个列表中使用不同的继承接口

发布于 2025-02-12 18:39:00 字数 1680 浏览 1 评论 0原文

目标

我想让许多演员使用可以在运行时交换的各种选择器在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的继承接口之一。因此,想到了一些想法,但是每个想法都有一些问题:

  1. make iActor generic ... eg iactor&lt; t&gt;其中t:iPosselector
    • 但是,我该如何将它们放在列表中?我需要一个常见的接口,需要在那里有Iselector,因此原始问题仍然存在。
  2. 使所有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:

  1. 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.
  2. Make all IActor implementations use the base IPosSelector, and then cast internally to IObjectSelector 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文