构建新的层次结构,使用抽象类还是接口?
我正准备为我的游戏构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
接口和抽象类用于两种不同的用途 - 接口用于定义契约,而抽象类用于提供基本实现并共享公共代码。 因此,您绝对应该始终使用接口,有时(我认为您的情况就是这种情况)还应该使用抽象类以避免重复代码。
因此,只有通过接口,您才会得到以下结果。
我假设您可以将公共节点特定代码分解为基类。
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.
I assume you can factor the common node specific code out into a base class.
区别在于,您可以放入抽象类的所有类之间是否存在公共代码,以及它们是否都需要具有此代码的同一组方法。 如果是这种情况,请使用抽象类。 如果没有通用代码,或者最终会让不应该通用的代码变得通用,那么就使用接口。
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.