循环依赖——总是错的?

发布于 2024-12-06 17:18:34 字数 445 浏览 3 评论 0原文

1.我想知道以下结构是否错误,原因是什么,解决办法是什么: 假设我已经实现了一个网络游戏客户端 客户端有2个主要包:
A.GUI - 持有所有的swing Jpanels等
B.LogicEngine

在逻辑引擎中,我有一个名为 clientThread 的类,其主要目标是与服务器通信以获取要在 Gui 面板上执行的命令,并根据用户在 Gui 面板上的选择结果发送回信息。

2为了做到这一点,我倾向于在 clientThread 中保留主 Gui 面板的引用,反之亦然,在不同项目的两个类之间进行循环引用是否如此错误?

3.在面向对象编程中,从类内部执行要在 Gui 上显示的内容(例如客户端线程,客户端线程负责以某种方式管理游戏流程,尽管它位于逻辑引擎包上)是否是错误的?

4.另外,如果 Gui 部分知道并使用逻辑部分,这是否有问题?

想听听一些建议
非常感谢

1.I would like to know if the following structure is incorrect, what is the reason, and what is the solution for that:
Assume I have implemented a client for net game
The client has 2 main packages:
A.GUI - hold all the swing Jpanels etc.
B.LogicEngine

In Logic engine I have a class called clientThread which main goal is to communicate with the server to get commands to execute on the Gui Panel and also to send information back as a result of the user choices on the Gui Panels..

2.In order to do so I tend to hold reference of my main Gui panel in clientThread and vise versa, is it so wrong to do cyclic reference between two classes of different projects?

3.Is it wrong in matter of object oriented programming to execute things to be shown on the Gui from within class like client thread which is responsible in some way to manage the flow of the game although it is on logical engine package?

4.Also if the Gui part know and uses the logical part is it a problem?

Would like to hear some advices
Thank you very much

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

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

发布评论

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

评论(2

亢潮 2024-12-13 17:18:34

显然,GUI 应该依赖于引擎,而不是相反(而且,上帝禁止,它们不应该相互依赖)。

您的问题实际上很常见并且很容易解决。引擎线程应该允许客户端代码安装一个侦听器,每次发生事件时都会收到通知。 GUI 实现该侦听器并安装它。请注意,游戏逻辑引擎仅了解侦听器接口,而不了解 GUUI 包中的特定实现。

这是观察者模式的实现,它有几个优点:

  • 通知代码(逻辑)不是与“感兴趣的”代码(GUI)相结合,引擎到 GUI 之间不存在依赖性,
  • 您可以插入侦听器/观察器的任何实现,例如将 Swing 应用程序更改为控制台/移动/Web 应用程序,而无需对引擎进行任何更改。
  • 你可以有多个监听器,例如一个用于更新 GUI,第二个用于运行声音等。

最后,从逻辑线程操作 GUI 没有任何问题,但你必须注意 事件调度线程

Obviously the GUI should depend on the engine, not the other way around (and, god forbid, they should not depend on each other).

Your problem is actually pretty common and simple to solve. The engine thread should allow client code to install a listener that will be notified every time something happens. Than GUI implements that listener and installs it. Note that the game logic engine is only aware of the listener interface, not the particular implementation that lies in your GUUI package.

This is an implementation of the Observer pattern and it has several advantages:

  • notifying code (logic) is not coupled with the "interested" code (the GUI), there is not dependency from engine to GUI
  • you can plug any implementation of listener/observer, for instance changing Swing application to console/mobile/web application without any change to the engine.
  • you can have several listeners, e.g. one to update the GUI, second one to run sound, etc.

Finally there is nothing wrong with manipulating your GUI from logic thread, hoever you must be aware of event dispatching thread.

双马尾 2024-12-13 17:18:34
  1. 我会添加第三个元素。我会有 GUI、LogicEngine 和通信包。通过这种方式,您可以使用文件、本地数据库或模拟类进行测试。逻辑和套接字不属于一起。它们只是彼此的输入和输出。
  2. 我个人希望逻辑对 GUI 一无所知。 GUI 的工作只是为了调用逻辑。 GUI 不知道谁或什么正在调用它,也不会关心。这就像微波炉不关心我或我妻子使用它的原因一样。
  3. 我不太明白这个问题。你能重新表述一下吗?
  4. 不,反过来才是问题所在。 GUI 的存在是为了让用户可以操纵逻辑。当逻辑依赖于 GUI 时,就会发生不好的事情。
  1. I would add a third element in. I would have GUI, LogicEngine, and Communication packages. In this way, you could do testing using files, or a local database, or mock classes. Logic and sockets don't belong together. They are simply inputs and outputs of each other.
  2. I personally would have the logic know nothing about the GUI. The GUI's job would exist only to make calls into the logic. The GUI doesn't know who or what is calling into it, nor would it care. It's the same reason a microwave doesn't care if I am using it or my wife.
  3. I don't quite understand this question. Could you rephrase it?
  4. No, the other way around is the problem. The GUI exists so the user can manipulate the logic. The bad things happen when the logic depends on the GUI.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文