问题描述
我正在为使用 javaFX 的 MVC 概念而苦苦挣扎.我正在使用 fxml 文件构建一个 javaFX 应用程序.
I'm struggling with the MVC concept using javaFX. I am building an javaFX application using fxml files.
每个 fxml 文件都分配了一个控制器,但我不认为这个控制器是 MVC 模式所述的控制器.我认为它就像某种 ViewController,它保存对 fxml 对象(按钮、窗格等)的引用.
Each fxml file has a controller assigned, but I don't think that this controller is one as the MVC pattern states. I think of it like some sort of ViewController, which holds references to fxml objects (buttons, panes etc.).
我的问题是:这个ViewController"和真正的Controller"之间的区别到底在哪里.什么对象应该做什么事?我在哪里设置例如动作监听器?
My problem is: Where exactly is the difference between this "ViewController" and the real "Controller". What object should do what things? Where do I set e.g. actionListeners?
推荐答案
关于 MVC 的思考
MVC 是一个定义相当松散的模式,它对 MVC 中的每个事物(尤其是控制器)所代表的含义开放(通常有些模糊).Martin Fowler 对 GUI 工具包进行了精彩的对 MVC 架构的讨论.
MVC is a pretty loosely defined pattern which is open to (often somewhat vague) interpretations of what each of the things in MVC stand for (especially the controller). There is a great discussion of the MVC architecture with respect to GUI toolkits by Martin Fowler.
关于设计模式和 FXML
基于 JavaFX 核心 FXML 的处理更像是一个工具包,而不是一个完整的开发框架.其他框架可以在 JavaFX 和 FXML 以及底层 JavaFX/FXML 实现和它们的控制器之上分层的想法不会在更高级别的框架上推动任何类型的议程或架构约束.
JavaFX core FXML based processing is built to be more of a toolkit rather than a complete development framework. The idea being that other frameworks could be layered on top of JavaFX and FXML and the underlying JavaFX/FXML implementations and the controllers for them would not push any kind of agenda or architectural constraints on the higher level frameworks.
因此,对基于 FXML 的核心处理及其控制器到 MVC 架构进行了故意松散的类比和映射.
As a result, there is a deliberately loose analogy and mapping of core FXML based processing and their controllers to an MVC architecture.
见 JavaFX 和 MVP – 设计模式的大杂烩 供进一步讨论.
考虑使用更高级别的框架
您可能会受益于采用有意见的"JavaFX 框架,例如 afterburner.fx,它利用控制器和 FXML,但提供更像是一个严格的结构化框架.Afterburner.fx 遵循 模型视图展示器 (MVP) 模型.尽管 Afterburner.fx 提供了比普通核心 JavaFX+FXML 更多的功能和结构,但它通过添加一些您需要学习使用它的额外类和 API 以最小的方式实现.
You might benefit from adopting an "opinionated" JavaFX framework such as afterburner.fx, which utilizes controllers and FXML but provides a bit more of a rigid structured framework. Afterburner.fx follows a Model View Presenter (MVP) model. Though Afterburner.fx provides more functionality and structure than plain core JavaFX+FXML, it does so in a minimal way by adding few extra classes and APIs that you need to learn to use it.
这篇关于MVC 与 javaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!