MVC 和 Java GUI 监听器

发布于 2024-10-14 22:37:42 字数 93 浏览 3 评论 0原文

我想问一下,从设计模式的角度来看,将GUI的监听器放在“视图”还是“控制器”中是否会更好。一位同事认为“观”是最自然的地方,但我不太确定。

I would like to ask whether from a design pattern point of view whether it would be better to place listeners for GUI within the "view" or the "controller". A colleague believes that the "view" is the most natural place, but i'm not so sure.

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

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

发布评论

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

评论(1

怕倦 2024-10-21 22:37:42

如果您正在谈论 Swing,则如 之前讨论过,Java中的MVC并不是一个清晰简单的该模式表明。那么,要回答您的问题,取决于您如何定义特定应用程序的“视图”和“控制器”,以及“放置侦听器”在其中一个或另一个中的含义。

我认为侦听器是控制器机制的一部分 - 它们在视图(显示当前状态)和模型(维护当前状态)之间提供松散(ish)耦合,并为两者提供一种方法相互影响。然而,大多数 Swing 侦听器与 UI 事件紧密绑定 - 单击鼠标按钮、从列表中选择项目等 - 因此您可能需要创建一个额外的抽象层来获取这些由侦听器捕获的 UI 事件,并将它们转换为对您的应用程序领域更通用的内容。例如,EJB 可以为某些业务逻辑提供通用接口,这些业务逻辑可以通过 Web 服务由 Swing UI 或 API 调用触发。那么,控制器就是 EJB,触发对该 EJB 的调用的 Swing 事件侦听器位于视图中。

If you are talking about Swing then, as previously discussed, MVC in Java is not a clear and simple as the pattern suggests. To answer your question, then, depends on how you define "view" and "controller" with respect to a specific application, and what you mean by "placing listeners" in one or the other.

I take the view the listeners are part of the controller mechanism - they provide a loose(ish) coupling between the view (that displays the current state) and the model (that maintains the current state), and provide a way for the two to interact. However, most Swing listeners are very tightly bound to UI events - mouse buttons being clicked, items being selected from lists, etc. - and so you may want to create an additional layer of abstraction which takes these UI events, which are captured by listeners, and translates them into something more general to the domain of your application. An EJB, for example, can provide a common interface for some business logic which may be triggered by a Swing UI or an API call via a web service. The controller, then, is the EJB, and the Swing event listener that triggers a call to that EJB is in the view.

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