如何在大型 JavaFx 2.0 应用程序中组织控制器/呈现器?

发布于 2024-12-25 13:31:39 字数 492 浏览 2 评论 0原文

对于一个项目,我一直在开发 JavaFX 2.0 桌面应用程序(一个 keytool UI)。 JavaFx2.0 的工作方式(无论如何在我的项目中),UI 事件处理发生在 JavaFX 2.0 UI 类本身中(例如:onclicked() 事件或属性更改侦听器)。

现在,我使用一个带有 getController() 方法的静态类,所有 UI 类都使用该方法来访问应用程序的一个控制器(不知何故,将控制器传递给所有 50 多个 UI 类对我来说似乎很混乱)。

然而问题是那个控制器变得非常大!它有太多的方法(所有业务逻辑方法都需要由我的 UI 类访问)。尽管它只将方法调用传递给我的模型/服务,但仍然需要在控制器级别捕获许多异常,以便在 UI 中处理它们(显示错误消息等)。

有人知道一种干净的方法可以使整个 MVC/MVP 模式更好地为我的应用程序工作,而无需 UI/控制器/模型类直接相互依赖吗?也许每个用例都有不同的控制器?但是我该如何才能让正确的 UI 类在不直接知道的情况下获得正确的控制器呢?也许使用接口?

For a project I've been working on a JavaFX 2.0 desktop application (a keytool UI). The way JavaFx2.0 works (in my project anyways), the UI event handling takes place in the JavaFX 2.0 UI classes itself (for example: onclicked() events or property change listeners).

Right now I use a static class with a method: getController(), which all UI classes use to access the one controller of the application (somehow it seemed messy to me to pass the controller on to all 50+ UI classes).

The problem is however that that one controller is getting very large! It has way too many methods (all business logic methods that need to be accessed by my UI classes). Even though it only passes the methods calls on to my model/service, there is still a lot of exceptions that need to be caught on controller level for handling them in the UI (show error messages etc).

Anyone know of a clean way to make this whole MVC/MVP pattern work better for my application without the UI / Controller / Model classes being directly depending on eachother? Maybe a different controller for each use case? But then how would I make it so that the right UI class gets the right Controller without directly knowing it? Maybe using an interface?

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

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

发布评论

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

评论(4

椒妓 2025-01-01 13:31:39

我不太了解 Java FX,所以您应该对我的回答持保留态度。我看了一些 Java FX 教程,但它们似乎都是很小的示例,没有任何类型的体系结构……MVC 或其他。

也许您不应该太努力地尝试拥有干净的 MVC 模式。在我看来,每个 UI 元素本身就是一个 MVC 单元,例如,标签包含文本(模型)、图形表示(视图)以及处理事件(控制器)。

尝试拥有一个单独的全局控制器可能会让你的生活更加痛苦。

但是,您可能会保留一个单独的模型,例如,如果您要显示数据库(模型)的内容,则这是必要的。但是,如果您正在做一些非常简单的事情,只需使用 UI 的状态作为您的模型就足够了;将数据(模型)单独保留在程序中只会让您浪费时间同步 UI 中的数据和单独模型中的数据。数据重复是邪恶的,应该尽可能避免。

I don't really know Java FX, so you should take my answer with a grain of salt. I looked a bit at Java FX tutorials, but they all seem to be tiny examples with no architecture of any kind... MVC or other.

Maybe you should not try too hard to have a clean MVC pattern. It seems to me that each UI element is itself a MVC unit, e.g. a Label contains text (the model), its graphical representation (the view) and it handles events (the controller).

You might just be making your life more painful by trying to have a separate global controller.

You might however keep a separate model, which would be necessary for example if you are showing the content of a database (the model). However if you are doing something quite simple, just using the state of the UI as your model would be sufficient; keeping the data (model) separately in the program would just make you waste time synchronizing the data in the UI's and the data in the separate model. Data duplication is evil and should be avoided as much as possible.

奶气 2025-01-01 13:31:39

我建议您测试 JRebirth 框架,

它提供了一个简单但功能强大的模式,可以帮助您构建应用程序。

该框架还很年轻,将根据收到的反馈进行改进。不要犹豫,发送一些。

http://www.jrebirth.org/doc/Overview.html

查看概述页面并提供源代码以了解更多信息。

还提供现场演示

I propose you to test the JRebirth Framework

It provide a simple but powerful pattern which will help you to structure your application.

This framework is young and will be improved according to feedback received. Don't hesitate to send some.

http://www.jrebirth.org/doc/Overview.html

Check the overview page and source code provided to learn more.

Live Demo are also available

屌丝范 2025-01-01 13:31:39

这里有一系列博客 在 JavaFX 中构建 JEE 应用程序2.0 这可能对你有帮助。它通过示例介绍了几种模式,说明如何在 JavaFx2 应用程序中解耦不同的组件 (MVP)。

There is a series of blogs here Building JEE applications in JavaFX 2.0 that may help you. It presents several patterns with example on how to decouple the different components (MVP) in an JavaFx2 application.

韵柒 2025-01-01 13:31:39

我在我的网站上放置了一个基本教程不久前讨论了如何使用 Java 和 Javafx 2 实现 MVC 模式。模型类始终是解耦的,不应该了解有关控制器和查看器的任何信息。如果这是一个大型项目,我建议使用包含模型、查看器和控制器的模块。模块可以是管理门户或谷歌地图查看器等。

http ://www.pergande.net/blog/article/post/Javafx+2.0+MVC/id/1

I have put a basic tutorial on my website for a while ago on how MVC pattern can be implemented using Java and Javafx 2. The model class is always decoupled and shouldn't know anything about the controller and viewer. If this is a large project I would recommend using modules where you have your model, viewer and controller in there. A module could be admin portal or google map viewer etc.

http://www.pergande.net/blog/article/post/Javafx+2.0+MVC/id/1

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