构建新的层次结构,使用抽象类还是接口?

发布于 2024-07-24 09:34:49 字数 437 浏览 1 评论 0原文

我正准备为我的游戏构建一个 2D 场景图,我想知道我是否应该在它的根使用一个接口或两个或几个抽象类。 这是我的要求:

  • 基本节点项
    • 需要能够存储矩阵
    • 还需要能够存储列表 子节点数
    • 以及单个父节点
  • 变换节点项
    • 需要有一个 Draw 方法(实现很可能是相同的)
    • 要求基本节点项是 实现/源自
  • 从Drawable 节点项
    • 需要有一个 Draw 方法(实现可能不同)
    • 要求基本节点项是 实施/派生自且不能 并肩实施/衍生 变换节点项

我应该使用什么基类/接口方案?

I'm preparing to build a 2D scene graph for my game, and i would like to know whether i should use at it's root an interface or two or a couple of abstract classes. Here's my requirements:

  • Base node item
    • needs to be able to store a matrix
    • also needs to be able to store a list
      of child nodes
    • as well as a single parent node
  • Transform node items
    • needs to have a Draw method (implementation is highly probable to be the same)
    • requires that the base node item be
      implemented/derived from
  • Drawable node item
    • needs to have a Draw method (implementation may be different)
    • requires that the base node item be
      implemented/derived from and cannot
      be implemented/derived from alongside
      the transform node item

What scheme of base classes/interfaces should i use for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-07-31 09:34:49

Jasonh 介绍了基础知识——在想要共享代码的地方使用抽象类,否则使用接口。

我想补充一点——即使您采用抽象类路线,我仍然建议创建一个接口。 你永远不知道什么时候你会想要一个像其他类一样工作的子类,但由于某种原因确实需要继承另一个类。 抽象基类非常适合保存实现代码。 不过,接口更加灵活,因此除非有充分的理由(即您的测试表明您无法承受虚拟方法调用的性能影响),否则请在有意义的情况下使用接口和抽象基类。

我对此的一般规则是:使用接口来定义 API,并使用抽象基类来允许接口的实现共享代码。

Jasonh covered the basics -- use the abstract class where you want to share code, otherwise use the interface.

I wan to add one point -- even if you go the abstract class route, I'd still recommend creating an interface as well. You never know when you'll want to have a subclass that acts like the others, but really needs to inherit another class for some reason. Abstract base classes are great to save implementation code. Interfaces are more flexible though, so unless there's a really good reason (ie. your testing reveals you can't take the performance hit of virtual method calls), use an interface as well as an abstract base class where it makes sense.

My general rule on this is: use interfaces to define the API, and use abstract base classes to allow implementations of the interface to share code.

最笨的告白 2024-07-31 09:34:49

接口和抽象类用于两种不同的用途 - 接口用于定义契约,而抽象类用于提供基本实现并共享公共代码。 因此,您绝对应该始终使用接口,有时(我认为您的情况就是这种情况)还应该使用抽象类以避免重复代码。

因此,只有通过接口,您才会得到以下结果。

class Transformation : INode, ITransformation { }
class GraphicsObject : INode, IGraphicsObject { }

我假设您可以将公共节点特定代码分解为基类。

abstarct class Node : INode { }

class Transformation : Node, ITransformation { }
class GraphicsObject : Node, IGraphicsObject { }

Interfaces and abstract classes are used for two different things - interface are used for defining contract while abstract class are used to provide a base implementation and share common code. So you should definitly always use interfaces and sometimes (and I think your case is such a case) also abstract classes to avoid duplicating code.

So with interfaces only you will get the following.

class Transformation : INode, ITransformation { }
class GraphicsObject : INode, IGraphicsObject { }

I assume you can factor the common node specific code out into a base class.

abstarct class Node : INode { }

class Transformation : Node, ITransformation { }
class GraphicsObject : Node, IGraphicsObject { }
美人如玉 2024-07-31 09:34:49

区别在于,您可以放入抽象类的所有类之间是否存在公共代码,以及它们是否都需要具有此代码的同一组方法。 如果是这种情况,请使用抽象类。 如果没有通用代码,或者最终会让不应该通用的代码变得通用,那么就使用接口。

The differentiator would be if there's common code between all the classes that you can put into an abstract class and if they all need the same set of methods with this code. If this is the case, go with the abstract class. If there's no common code or you would end up making code common that shouldn't be common, then go with an interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文