在我的吃豆人女士的实现中可能过度使用了 java 接口
过去 6 年里我一直是一名 Java 程序员,从今年年初开始我对游戏编程产生了兴趣。因此,我认为从流行游戏入手是个好主意,于是我用 Java 实现了吃豆人女士游戏。我可以说我的实现看起来与原始游戏有 90% 相似,并且我尝试使用尽可能多的设计模式和最佳实践,因为这只是一个学习编写基本 2D 游戏代码的个人项目。
现在我完成了编码,我意识到我有 19 个接口,而只有 17 个类!所以我开始想知道我是否可能过度使用接口。
以下是我使用的几个类/接口的示例:
Class - FullGame(实现 FullGameInterface 和 FullGameObservable)
Class - View1(实现 FullGameObserver)
Interface - FullGameInterface(基本功能方法:恢复、暂停、播放等)
接口 - FullGameObservable(允许注册视图以获取更新通知)
接口 - FullGameObserver(实现通过游戏的 2 个不同视图来接收通知)
我是否过度使用界面?
你的意见是什么?
I have been a Java programmer for the last 6 years, and since the beginning of this year I got interested in game programming. So, I thought it was a good idea to start with a popular game and I implemented the game Ms. Pac-Man in Java. I can say that my implementation looks about 90% similar to the original game, and I tried to used as much design patterns and best practices as possible, since this was just a personal project to learn to code basic 2D games.
Now that I finished the coding, I realized that I have 19 interfaces and just 17 classes! So I start wondering if I might be overusing interfaces.
Here is an example of a few classes/interfaces that I use:
Class - FullGame (implements FullGameInterface and FullGameObservable)
Class - View1 (implements FullGameObserver)
Interface - FullGameInterface (basic functionality methods: resume, pause, play, etc.)
Interface - FullGameObservable (allows views to be registered for update notification)
Interface - FullGameObserver (implemented by the 2 different views of the game to receive notifications)
Am I overusing interfaces?
What is your opinion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要更改实现,请使用接口。
对于像 FullGame 对象这样的东西,您可能不需要该接口。
如果您要更改 FullGame 的功能,那么您可以考虑创建一个接口,以便可以切换为 FullGameInterface 实例化的对象。
编辑2:仅在需要时使代码变得更复杂。如果您认为需要一个接口,请先停止。使用您已有的课程。 (但是当您使用它时,请尝试让调用者远离实现细节。)一旦您有了第二个类似的类,那么您就可以弄清楚什么是真正的接口,什么是实现。如果您的调用者仍然需要难以放入通用接口的实现细节,那么您就没有将它们保持足够清晰的分离。
编辑以获得更完整的答案:
接口对于将要访问对象(接口)的方法与实际对象(实现)解耦很有用。这适用于多种情况:
List
、ArrayList
和LinkedList
。List
是通用接口,允许许多基于集合的方法接受ArrayList
或LinkedList
。Collection
接口允许对所有集合进行类似的排列。Image
和两个实现者:FileImage
和FileImageProxy
。代理按需加载FileImage
并将对Image
接口的调用传递给实际对象。客户永远不知道也不关心他们如何获得图像,或者图像是否真正加载;他们只知道有一个Image
并且可以使用它。Use an interface if you want to change the implementation.
For something like your FullGame object, you probably don't need the interface.
If you were to ever change the functionality of FullGame, then you could consider making an interface, so that you can switch which object you instantiate for FullGameInterface.
EDIT 2: Only make the code more complex when you need to. If you think you need an interface, stop first. Use the class that you have already. (But while you use it, try to keep the callers out of the implementation details.) Once you have a second similar class, then you can figure out what's really interface and what's implementation. If your callers still need implementation details that are awkward to put in the common interface, then you're not keeping them cleanly separated enough.
EDIT for a more full answer:
Interfaces are useful for decoupling the methods by which you want to access an object (the interface) from the actual object (the implementation). This is good for several situations:
List
,ArrayList
, andLinkedList
.List
is the general interface that allows many collections-based methods to accept either anArrayList
or aLinkedList
. TheCollection
interface permits a similar arrangement for all collections.Image
and two implementers,FileImage
andFileImageProxy
. The proxy loads theFileImage
on demand and passes calls to theImage
interface on to the actual object. The clients never know or care how they get the image, or whether it's really loaded or not; they just know that there's anImage
and they can use it.如果您可以控制代码并且只有一种实现,那么这是一个好兆头,表明该接口毫无意义(尽管如果您想进行测试,它可能会很有用)。
请记住,内部类也是类。
接口和实现的多重继承通常是一个坏主意。
If you are in control of the code and there is only one implementation, that it is a good sign that the interface is pointless (although it might be useful if you want to, say, test).
Remember that inner classes are classes too.
Multiple inheritance of interface, as well as implementation, is usually a bad idea.
接口很好。拥有很多并不是问题。
特别是如果您刚刚实现了第一款游戏。如果您重新使用代码库来构建更多游戏,您可能最终会得到更多的多态性,因为您将重新使用接口。但此时,如果实现中的方法多于接口中的方法,那么接口就达到了目的——它们向客户端隐藏了实现细节,从而迫使客户端遵守接口,实现多态性才是 OO 的真正好处。
Interfaces are good. It's not a problem to have a lot of them.
Particularly if you've only implemented your first game. If you re-used the code base to build more games, you would probably end up with more polymorphism, because you'd be re-using the interfaces. But at this point, if it's the case that there are more methods in the implementations than in the interfaces, then the interfaces are serving a purpose--they're hiding implementation details from the clients, who are thereby forced to conform to the interface, enabling the polymorphism that is the real benefit of OO.
我经常使用界面来充实我的设计。我彻底地记录了它们,这迫使我思考我的班级的责任。 API 和类的内部结构之间总是存在一些区别,因此即使您还没有多个实现,指定一个接口通常也不会造成什么影响。
I often use interfaces to flesh out my design. I document them thoroughly and this forces me to think through the responsibilities of my classes. There is always some distinction between the API and the internals of a class, so even if you don't have multiple implementations of (yet) it generally does not hurt to specify an interface.