在我的吃豆人女士的实现中可能过度使用了 java 接口

发布于 2024-08-07 00:04:30 字数 657 浏览 6 评论 0原文

过去 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 技术交流群。

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

发布评论

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

评论(4

彻夜缠绵 2024-08-14 00:04:30

如果要更改实现,请使用接口。

对于像 FullGame 对象这样的东西,您可能不需要该接口。

如果您要更改 FullGame 的功能,那么您可以考虑创建一个接口,以便可以切换为 FullGameInterface 实例化的对象。

编辑2:仅在需要时使代码变得更复杂。如果您认为需要一个接口,请先停止。使用您已有的课程。 (但是当您使用它时,请尝试让调用者远离实现细节。)一旦您有了第二个类似的类,那么您就可以弄清楚什么是真正的接口,什么是实现。如果您的调用者仍然需要难以放入通用接口的实现细节,那么您就没有将它们保持足够清晰的分离。

编辑以获得更完整的答案:

接口对于将要访问对象(接口)的方法与实际对象(实现)解耦很有用。这适用于多种情况:

  1. 这些方法有多个实现,具有相同的语义和一般操作,但具有不同的底层实现。例如,ListArrayListLinkedListList 是通用接口,允许许多基于集合的方法接受 ArrayListLinkedListCollection 接口允许对所有集合进行类似的排列。
  2. 出于架构原因,您需要将接口与实现分开。例如,您可以将调用对象放在一台机器上,将实现对象放在另一台机器上。调用机器上的代理实现通过网络调用真实对象的接口。该接口意味着调用者不必知道其中的区别。 Java RMI 就是这么做的。
  3. 您需要能够修改调用对象的方式。例如,采用一个接口 Image 和两个实现者:FileImageFileImageProxy。代理按需加载 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:

  1. You have more than one implementation of those methods, with the same semantics and general operations, but with different underlying implementations. For example, List, ArrayList, and LinkedList. List is the general interface that allows many collections-based methods to accept either an ArrayList or a LinkedList. The Collection interface permits a similar arrangement for all collections.
  2. You need to separate the interface from the implementation for architectural reasons. For example, you could have the calling object on one machine and the implementing object on another. A proxy on the calling machine implements the interface to call across the network to the real object. The interface means that the caller doesn't have to know the difference. Java RMI did this.
  3. You need to be able to modify the way that the object is called. For example, take an interface Image and two implementers, FileImage and FileImageProxy. The proxy loads the FileImage on demand and passes calls to the Image 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 an Image and they can use it.
蹲在坟头点根烟 2024-08-14 00:04:30

如果您可以控制代码并且只有一种实现,那么这是一个好兆头,表明该接口毫无意义(尽管如果您想进行测试,它可能会很有用)。
请记住,内部类也是类。

接口和实现的多重继承通常是一个坏主意。

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.

羞稚 2024-08-14 00:04:30

接口很好。拥有很多并不是问题。

特别是如果您刚刚实现了第一款游戏。如果您重新使用代码库来构建更多游戏,您可能最终会得到更多的多态性,因为您将重新使用接口。但此时,如果实现中的方法多于接口中的方法,那么接口就达到了目的——它们向客户端隐藏了实现细节,从而迫使客户端遵守接口,实现多态性才是 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.

机场等船 2024-08-14 00:04:30

我经常使用界面来充实我的设计。我彻底地记录了它们,这迫使我思考我的班级的责任。 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.

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